简体   繁体   English

ToString(“N2”)和ToString(“0.00”)之间的区别

[英]Difference between ToString(“N2”) and ToString(“0.00”)

ToString("N2")ToString("0.00")之间有什么区别?

From Standard Numeric Format Strings 来自标准数字格式字符串

The number is converted to a string of the form "-d,ddd,ddd.ddd…", where '-' indicates a negative number symbol if required, 'd' indicates a digit (0-9), ',' indicates a thousand separator between number groups, and '.' 该数字将转换为“-d,ddd,ddd.ddd ...”形式的字符串,其中“ - ”表示负数字符号(如果需要),“d”表示数字(0-9),','表示数字组之间的千位分隔符和'。' indicates a decimal point symbol. 表示小数点符号。

It would seem that N will include thousands separators, whereas 0.00 will not. 似乎N将包括数千个分隔符,而0.00则不包括。

See also Custom Numeric Format Strings 另请参见自定义数字格式字符串

Basically, ToString("N2") will use the CultureInfo to format the number. 基本上, ToString("N2")将使用CultureInfo格式化数字。 This means that your thousands separator might be different depending on the used CultureInfo . 这意味着您的千位分隔符可能会有所不同,具体取决于使用的CultureInfo You can also pass the desired CultureInfo if you want. 如果需要,您还可以传递所需的CultureInfo

It's all about the decimal places 这都是关于小数位的

N2 will work the same way for 500.00, but when you have 5000.00, N2 will display as N2将以相同的方式运行500.00,但是当你有5000.00时, N2将显示为

5,000.00 5,000.00

instead of 代替

5000.00 5000.00

See Standard Numeric Format Strings for more information. 有关更多信息,请参阅标准数字格式字符串

Both give you two decimal places, but you can see the difference easily if you check larger numbers: 两者都给你两个小数位,但如果检查更大的数字,你可以很容易地看到差异:

var d = 1234567.89;
for (var i = 0; i < 10; ++i) {
    Console.WriteLine(d.ToString("N2") + "\t" + d.ToString("0.00"));
    d /= 10.0;
}

outputs 输出

1.234.567,89    1234567,89
123.456,79  123456,79
12.345,68   12345,68
1.234,57    1234,57
123,46  123,46
12,35   12,35
1,23    1,23
0,12    0,12
0,01    0,01
0,00    0,00

Execute code online at dotnetfiddle.net 在dotnetfiddle.net在线执行代码

The thousand separator is an issue. 千分隔符是一个问题。 Using "n2" will give you 3,543 where using "0.00" will give you 3543. The comma can break down-stream code that might have to parse that value back to a decimal, especially client-side js. 使用“n2”将为您提供3,543,其中使用“0.00”将为您提供3543.逗号可以分解下游代码,可能必须将该值解析回小数,尤其是客户端js。

Here is example to explain 这是一个例子来解释

int rupees=567.3423;
string rp=rupees.tostring("N2");

--Answer is rp="567.34"; - 答案是rp="567.34"; -- N2 gives upto two decimal records. - N2最多提供两个十进制记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM