简体   繁体   English

双字符串转换

[英]double to string conversion

I am using the below code: 我正在使用以下代码:

string.Format("{0:###,###,###,###,###.00}",12005557590928.143);

to convert the double value to string. 将double值转换为字符串。

it gives the output as "12,005,557,590,928.10" 它的输出为“ 12,005,557,590,928.10”

if I change it to 如果我将其更改为

string.Format("{0:###,###,###,###,###.##}",12005557590928.143);

I get. 知道了 "12,005,557,590,928.1". “ 12,005,557,590,928.1”。

How can I get the output as "12005557590928.143"? 如何获得输出为“ 12005557590928.143”?

You're seeing a precision error because of the data type you're using for the number. 由于数字所使用的数据类型,您会看到一个精度错误。 Try using a fixed point number type (in this case, a decimal ): 尝试使用定点数字类型(在这种情况下为decimal ):

string.Format("{0:###,###,###,###,###.###}", 12005557590928.143m);

I believe you want string.Format("{0:###,###,###,###,###.000}",12005557590928.143); 我相信您想要string.Format("{0:###,###,###,###,###.000}",12005557590928.143); (note the extra zero at the end) (请注意最后的额外零)

You can use 您可以使用

(12005557590928.143).ToString("R")

The custom numeric format strings will never reveal more than 15 decimal digits of a System.Double . 自定义数字格式字符串将永远不会显示System.Double 15个以上的十进制数字。 Consider using System.Decimal if you need greater precision. 如果需要更高的精度,请考虑使用System.Decimal

If your goal is to reveal "hidden" digits of the Double , use standard numeric format strings "R" ("round-trip") or "G17" ("general" 17 digits). 如果您的目标是显示Double “隐藏”数字,请使用标准数字格式字符串"R" (“往返”)或"G17" (“常规” 17位数字)。

Try with .ToString("R") 尝试使用.ToString("R")

Console.WriteLine((12005557590928.143).ToString("R"));

Here is a DEMO . 这是一个DEMO

Console.Write(string.Format("{0:0.000}", (12005557590928.143m)));

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

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