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

前端 未结 17 2148
没有蜡笔的小新
没有蜡笔的小新 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-22 00:11

    https://msdn.microsoft.com/en-us/library/dwhawy9k%28v=vs.110%29.aspx

    This link explains in detail how you can handle your problem and what you can do if you want to learn more. For simplicity purposes, what you want to do is

    double whateverYouWantToChange = whateverYouWantToChange.ToString("F2");
    

    if you want this for a currency, you can make it easier by typing "C2" instead of "F2"

    0 讨论(0)
  • 2020-11-22 00:13

    Given decimal d=12.345; the expressions d.ToString("C") or String.Format("{0:C}", d) yield $12.35 - note that the current culture's currency settings including the symbol are used.

    Note that "C" uses number of digits from current culture. You can always override default to force necessary precision with C{Precision specifier} like String.Format("{0:C2}", 5.123d).

    0 讨论(0)
  • 2020-11-22 00:15

    You can use system.globalization to format a number in any required format.

    For example:

    system.globalization.cultureinfo ci = new system.globalization.cultureinfo("en-ca");
    

    If you have a decimal d = 1.2300000 and you need to trim it to 2 decimal places then it can be printed like this d.Tostring("F2",ci); where F2 is string formating to 2 decimal places and ci is the locale or cultureinfo.

    for more info check this link
    http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx

    0 讨论(0)
  • 2020-11-22 00:17

    There's already two high scoring answers that refer to Decimal.Round(...) but I think a little more explanation is needed - because there's an unexpected important property of Decimal that isn't obvious.

    A Decimal 'knows' how many decimal places it has based upon where it came from

    For instance the following may be unexpected :

    Decimal.Parse("25").ToString()          =>   "25"
    Decimal.Parse("25.").ToString()         =>   "25"
    Decimal.Parse("25.0").ToString()        =>   "25.0"
    Decimal.Parse("25.0000").ToString()     =>   "25.0000"
    
    25m.ToString()                          =>   "25"
    25.000m.ToString()                      =>   "25.000"
    

    Doing the same operations with Double will give no decimal places ("25") for each of the above.

    When you want a decimal to 2 decimal places theres about a 95% chance it's because it's currency in which case this is probably fine for 95% of the time:

    Decimal.Parse("25.0").ToString("c")     =>   "$25.00"
    

    Or in XAML you just use {Binding Price, StringFormat=c}

    One case I ran into where I needed a decimal AS a decimal was when sending XML to Amazon's webservice. The service was complaining because a Decimal value (originally from SQL Server) was being sent as 25.1200 and rejected, (25.12 was the expected format).

    All I needed to do was Decimal.Round(...) with 2 decimal places to fix the problem.

     // This is an XML message - with generated code by XSD.exe
     StandardPrice = new OverrideCurrencyAmount()
     {
           TypedValue = Decimal.Round(product.StandardPrice, 2),
           currency = "USD"
     }
    

    TypedValue is of type Decimal so I couldn't just do ToString("N2") and needed to round it and keep it as a decimal.

    0 讨论(0)
  • 2020-11-22 00:17

    Math.Round Method (Decimal, Int32)

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