How do I display a decimal value to 2 decimal places?

前端 未结 17 2146
没有蜡笔的小新
没有蜡笔的小新 2020-11-21 23:24

When displaying the value of a decimal currently with .ToString(), it\'s accurate to like 15 decimal places, and since I\'m using it to represent dollars and ce

相关标签:
17条回答
  • 2020-11-21 23:52

    None of these did exactly what I needed, to force 2 d.p. and round up as 0.005 -> 0.01

    Forcing 2 d.p. requires increasing the precision by 2 d.p. to ensure we have at least 2 d.p.

    then rounding to ensure we do not have more than 2 d.p.

    Math.Round(exactResult * 1.00m, 2, MidpointRounding.AwayFromZero)
    
    6.665m.ToString() -> "6.67"
    
    6.6m.ToString() -> "6.60"
    
    0 讨论(0)
  • 2020-11-21 23:54

    Mike M.'s answer was perfect for me on .NET, but .NET Core doesn't have a decimal.Round method at the time of writing.

    In .NET Core, I had to use:

    decimal roundedValue = Math.Round(rawNumber, 2, MidpointRounding.AwayFromZero);
    

    A hacky method, including conversion to string, is:

    public string FormatTo2Dp(decimal myNumber)
    {
        // Use schoolboy rounding, not bankers.
        myNumber = Math.Round(myNumber, 2, MidpointRounding.AwayFromZero);
    
        return string.Format("{0:0.00}", myNumber);
    }
    
    0 讨论(0)
  • 2020-11-21 23:55
    decimalVar.ToString("F");
    

    This will:

    • Round off to 2 decimal places eg. 23.45623.46
    • Ensure that there are always 2 decimal places eg. 2323.00; 12.512.50

    Ideal for displaying currency.

    Check out the documentation on ToString("F") (thanks to Jon Schneider).

    0 讨论(0)
  • 2020-11-21 23:58

    Here is a little Linqpad program to show different formats:

    void Main()
    {
        FormatDecimal(2345.94742M);
        FormatDecimal(43M);
        FormatDecimal(0M);
        FormatDecimal(0.007M);
    }
    
    public void FormatDecimal(decimal val)
    {
        Console.WriteLine("ToString: {0}", val);
        Console.WriteLine("c: {0:c}", val);
        Console.WriteLine("0.00: {0:0.00}", val);
        Console.WriteLine("0.##: {0:0.##}", val);
        Console.WriteLine("===================");
    }
    

    Here are the results:

    ToString: 2345.94742
    c: $2,345.95
    0.00: 2345.95
    0.##: 2345.95
    ===================
    ToString: 43
    c: $43.00
    0.00: 43.00
    0.##: 43
    ===================
    ToString: 0
    c: $0.00
    0.00: 0.00
    0.##: 0
    ===================
    ToString: 0.007
    c: $0.01
    0.00: 0.01
    0.##: 0.01
    ===================
    
    0 讨论(0)
  • 2020-11-21 23:59

    If you want it formatted with commas as well as a decimal point (but no currency symbol), such as 3,456,789.12...

    decimalVar.ToString("n2");
    
    0 讨论(0)
  • 2020-11-22 00:01

    If you just need this for display use string.Format

    String.Format("{0:0.00}", 123.4567m);      // "123.46"
    

    http://www.csharp-examples.net/string-format-double/

    The "m" is a decimal suffix. About the decimal suffix:

    http://msdn.microsoft.com/en-us/library/364x0z75.aspx

    0 讨论(0)
提交回复
热议问题