当显示当前使用.ToString()的十进制值时,它精确到15位小数,因为我使用它来表示美元和美分,所以我只希望输出为2位小数。
我是否为此使用.ToString()的变体?
当显示当前使用.ToString()的十进制值时,它精确到15位小数,因为我使用它来表示美元和美分,所以我只希望输出为2位小数。
我是否为此使用.ToString()的变体?
当前回答
给定小数d=12.345;表达式d.ToString(“C”)或String.Format(“{0:C}”,d)产生$12.35-请注意,使用了当前区域性的货币设置,包括符号。
请注意,“C”使用当前区域性中的位数。您可以始终使用C{precision说明符}(如String.Format(“{0:C2}”,5.123d))重写默认值以强制执行必要的精度。
其他回答
给定小数d=12.345;表达式d.ToString(“C”)或String.Format(“{0:C}”,d)产生$12.35-请注意,使用了当前区域性的货币设置,包括符号。
请注意,“C”使用当前区域性中的位数。您可以始终使用C{precision说明符}(如String.Format(“{0:C2}”,5.123d))重写默认值以强制执行必要的精度。
如果值为0,则很少需要空字符串。
decimal test = 5.00;
test.ToString("0.00"); //"5.00"
decimal? test2 = 5.05;
test2.ToString("0.00"); //"5.05"
decimal? test3 = 0;
test3.ToString("0.00"); //"0.00"
排名最高的答案是错误的,浪费了(大多数)人10分钟的时间。
排名靠前的答案描述了一种格式化十进制值的字符串表示的方法,它是有效的。
但是,如果您确实想将保存的精度更改为实际值,则需要编写如下内容:
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"));
}
您可以使用system.globalization以任何所需格式格式化数字。
例如:
system.globalization.cultureinfo ci = new system.globalization.cultureinfo("en-ca");
如果您有一个小数d=1.2300000,并且需要将其修剪为2位小数,则可以像这样打印d.Tostring(“F2”,ci);其中F2是字符串格式,小数点后2位,ci是区域设置或文化信息。
有关详细信息,请查看此链接http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx
下面是一个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
===================