1

我有一个双精度值,我想将其转换为超过默认 15 位数字的字符串。我怎样才能做到这一点?

(1.23456789987654321d).ToString(); // 1.23456789987654
(12.3456789987654321d).ToString(); // 12.3456789987654
(1.23456789987654321d).ToString("0.######################################"); // 1.23456789987654
(1.23456789987654321d).ToString("0.0000000000000000000000000000000"); // 1.2345678998765400000000000000000
4

2 回答 2

6

我有一个双精度值,我想将其转换为超过默认 15 位数字的字符串。

为什么?15位数之后基本上就是垃圾了。您可以使用我的课程获得确切的DoubleConverter值:

string exact = DoubleConverter.ToExactString(value);

...但在 15 位数之后,剩下的只是噪音。

如果您想要超过 15 位有效数字的有意义数据,请使用decimal.

于 2013-04-03T22:08:57.197 回答
2

使用 double 是不可能的,因为它不支持超过 15 位的精度。您可以尝试使用十进制数据类型:

using System;

namespace Code.Without.IDE
{
    public class FloatingTypes
    {
        public static void Main(string[] args)
        {
            decimal deci = 1.23456789987654321M;
            decimal decix = 1.23456789987654321987654321987654321M;
            double doub = 1.23456789987654321d;
            Console.WriteLine(deci); // prints - 1.23456789987654321
            Console.WriteLine(decix); // prints - 1.2345678998765432198765432199
            Console.WriteLine(doub); // prints - 1.23456789987654
        }
    }
}
于 2013-04-03T22:22:59.593 回答