locked
Currency Formatting RRS feed

  • Question

  • User-1711956637 posted

    I have to Format a string to Currency Format, but that has to return as 1.0 M, 1.5 M, 2.0M, 2.5 M?

    Monday, July 8, 2013 7:00 AM

Answers

  • User281315223 posted

    Could you provide an example using input and output of what you are expecting? The Convert and Parse methods should be able to perform the appropriate conversion for you to a specific value.

    Although you'll likely need to convert the value to a specific figure and then format back into a string : 

    //A placeholder for your value
    decimal yourDecimalValue;
    
    //Your example string
    string yourStringValue = "1.5";
    
    //If the value could be successfully parsed as a decimal (then store it in the yourDecimalValue variable)
    if(Decimal.TryParse(yourStringValue, out yourDecimalValue))
    {
          //Handle your currency related formatting here
          string currencyString = yourDecimalValue.ToString("c"); //yields "$1.50"
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 8, 2013 8:10 AM
  • User-1373950060 posted

    Hi,

    When building a string for output to a web page, it’s useful to format any currency value in a human-friendly money format. This is extremely easy in C#.

    The system format string works like this: {0:C}

    For example, the following code example: 

    decimal moneyvalue = 1921.39m;
    string html = String.Format("Order Total: {0:C}", moneyvalue);
    Console.WriteLine(html);

    Please reference. Hope that's helpful.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 9, 2013 3:39 AM

All replies

  • User281315223 posted

    Could you provide an example using input and output of what you are expecting? The Convert and Parse methods should be able to perform the appropriate conversion for you to a specific value.

    Although you'll likely need to convert the value to a specific figure and then format back into a string : 

    //A placeholder for your value
    decimal yourDecimalValue;
    
    //Your example string
    string yourStringValue = "1.5";
    
    //If the value could be successfully parsed as a decimal (then store it in the yourDecimalValue variable)
    if(Decimal.TryParse(yourStringValue, out yourDecimalValue))
    {
          //Handle your currency related formatting here
          string currencyString = yourDecimalValue.ToString("c"); //yields "$1.50"
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 8, 2013 8:10 AM
  • User-1910946339 posted

    The string will either be Currency Format or it is 1.0 M, 1.5 M, 2.5 M.  Which one do you want?

    Currency -> string.Format("{0:c}", x);

    M format -> string.Format("{0:0.0} M", x);

    (Unless there is a currency somewhere in the world that is formatted the 'M' way.  If so could you give us the windows locale and we can show you how to use that to get your desired result)

    Monday, July 8, 2013 5:28 PM
  • User-1373950060 posted

    Hi,

    When building a string for output to a web page, it’s useful to format any currency value in a human-friendly money format. This is extremely easy in C#.

    The system format string works like this: {0:C}

    For example, the following code example: 

    decimal moneyvalue = 1921.39m;
    string html = String.Format("Order Total: {0:C}", moneyvalue);
    Console.WriteLine(html);

    Please reference. Hope that's helpful.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 9, 2013 3:39 AM