当显示当前使用.ToString()的十进制值时,它精确到15位小数,因为我使用它来表示美元和美分,所以我只希望输出为2位小数。

我是否为此使用.ToString()的变体?


当前回答

下面是一个Linqpad小程序,可以显示不同的格式:

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("===================");
}

以下是结果:

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
===================

其他回答

如果您只是需要显示,请使用string.Format

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

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

“m”是十进制后缀。关于十进制后缀:

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

        var arr = new List<int>() { -4, 3, -9, 0, 4, 1 };
        decimal result1 = arr.Where(p => p > 0).Count();
        var responseResult1 = result1 / arr.Count();
        decimal result2 = arr.Where(p => p < 0).Count();
        var responseResult2 = result2 / arr.Count();
        decimal result3 = arr.Where(p => p == 0).Count();
        var responseResult3 = result3 / arr.Count();
        Console.WriteLine(String.Format("{0:#,0.000}", responseResult1));
        Console.WriteLine(String.Format("{0:#,0.0000}", responseResult2));
        Console.WriteLine(String.Format("{0:#,0.00000}", responseResult3));

你可以放任意多的0。

所有这些都没有达到我所需要的效果,即强制2 d.p.并舍入为0.005-0.01

强制2 d.p.需要将精度提高2 d.p.,以确保至少有2 d.p。

然后四舍五入,以确保我们的d.p不超过2。

Math.Round(exactResult * 1.00m, 2, MidpointRounding.AwayFromZero)

6.665m.ToString() -> "6.67"

6.6m.ToString() -> "6.60"

排名靠前的答案描述了一种格式化十进制值的字符串表示的方法,它是有效的。

但是,如果您确实想将保存的精度更改为实际值,则需要编写如下内容:

public static class PrecisionHelper
{
    public static decimal TwoDecimalPlaces(this decimal value)
    {
        // These first lines eliminate all digits past two places.
        var timesHundred = (int) (value * 100);
        var removeZeroes = timesHundred / 100m;

        // In this implementation, I don't want to alter the underlying
        // value.  As such, if it needs greater precision to stay unaltered,
        // I return it.
        if (removeZeroes != value)
            return value;

        // Addition and subtraction can reliably change precision.  
        // For two decimal values A and B, (A + B) will have at least as 
        // many digits past the decimal point as A or B.
        return removeZeroes + 0.01m - 0.01m;
    }
}

单元测试示例:

[Test]
public void PrecisionExampleUnitTest()
{
    decimal a = 500m;
    decimal b = 99.99m;
    decimal c = 123.4m;
    decimal d = 10101.1000000m;
    decimal e = 908.7650m

    Assert.That(a.TwoDecimalPlaces().ToString(CultureInfo.InvariantCulture),
        Is.EqualTo("500.00"));

    Assert.That(b.TwoDecimalPlaces().ToString(CultureInfo.InvariantCulture),
        Is.EqualTo("99.99"));

    Assert.That(c.TwoDecimalPlaces().ToString(CultureInfo.InvariantCulture),
        Is.EqualTo("123.40"));

    Assert.That(d.TwoDecimalPlaces().ToString(CultureInfo.InvariantCulture),
        Is.EqualTo("10101.10"));

    // In this particular implementation, values that can't be expressed in
    // two decimal places are unaltered, so this remains as-is.
    Assert.That(e.TwoDecimalPlaces().ToString(CultureInfo.InvariantCulture),
        Is.EqualTo("908.7650"));
}

Decimal有一个非常重要的特点,这一点并不明显:

十进制“知道”它有多少个小数点,这取决于它来自哪里

以下可能是意外的:

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"

对Double执行相同的操作将导致上述所有示例的小数位数为零(“25”)。

如果你想要小数点到小数点后2位,那么可能性很高,这是因为它是货币,在这种情况下,95%的时间都可以:

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

或者在XAML中使用{Binding Price,StringFormat=c}

我遇到过一个需要十进制作为十进制的例子,那就是将XML发送到Amazon的Web服务时。服务正在抱怨,因为Decimal值(最初来自SQL Server)被发送为25.1200并被拒绝(25.12是预期格式)。

我所需要做的就是十进制。无论值的来源如何,用2个小数位舍入(…)来解决问题。

 // generated code by XSD.exe
 StandardPrice = new OverrideCurrencyAmount()
 {
       TypedValue = Decimal.Round(product.StandardPrice, 2),
       currency = "USD"
 }

TypedValue是Decimal类型,所以我不能只做ToString(“N2”),需要将其舍入并保持为十进制。