1

我正在使用以下代码:

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

将双精度值转换为字符串。

它给出的输出为“12,005,557,590,928.10”

如果我将其更改为

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

我明白了。“12,005,557,590,928.1”。

如何获得“12005557590928.143”的输出?

4

5 回答 5

3

由于您用于数字的数据类型,您会看到精度错误。尝试使用定点数类型(在本例中为 a decimal):

string.Format("{0:###,###,###,###,###.###}", 12005557590928.143m);
于 2013-01-09T15:48:07.600 回答
1

I believe you want string.Format("{0:###,###,###,###,###.000}",12005557590928.143); (note the extra zero at the end)

于 2013-01-09T15:45:52.943 回答
1

You can use

(12005557590928.143).ToString("R")

The custom numeric format strings will never reveal more than 15 decimal digits of a System.Double. Consider using System.Decimal if you need greater precision.

If your goal is to reveal "hidden" digits of the Double, use standard numeric format strings "R" ("round-trip") or "G17" ("general" 17 digits).

于 2013-01-09T15:46:58.793 回答
1

尝试.ToString("R")

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

这里有一个DEMO.

于 2013-01-09T15:47:54.197 回答
1
Console.Write(string.Format("{0:0.000}", (12005557590928.143m)));
于 2013-01-09T15:57:58.017 回答