[英]Decimal parse when the thousand separator and decimal separator are the same C#
[英]How to format a number in localized thousand separator keeping the decimal digits the same?
我有一些数字如下
double num1 = 123456789.2345;
decimal num2 = 123456789.2M;
我想用本地化的千位分隔符对它们进行格式化,保持小数位不变。 我试过使用下面的代码
NumberFormatInfo nfi = new CultureInfo( "en-US", false ).NumberFormat;
Console.WriteLine( num1.ToString( "N", nfi ) );
/* Changed Num1 to Num2 as per question*/
Console.WriteLine( num2.ToString( "N", nfi ) );
但它也会更改小数位数并产生以下 output。
123,456,789.23
123,456,789.20
任何帮助将不胜感激。 谢谢你
我想你想要这个ToString
Overload any numeric type supports:
decimal aNumber = 1234123.12;
string convertedValue = aNumber.ToString("n", new CultureInfo("en-US"));
convertedValue = 1,234,123.120
string convertedValue = aNumber.ToString("n2", new CultureInfo("it-CH")); //Switzerland
convertedValue = 1'234'123.12
参考: https://learn.microsoft.com/en-us/do.net/standard/base-types/standard-numeric-format-strings
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.