[英]How to format a double with more digits?
我有一個雙精度值,我想將其轉換為大於默認15位數字的字符串。 我該怎么做?
(1.23456789987654321d).ToString(); // 1.23456789987654
(12.3456789987654321d).ToString(); // 12.3456789987654
(1.23456789987654321d).ToString("0.######################################"); // 1.23456789987654
(1.23456789987654321d).ToString("0.0000000000000000000000000000000"); // 1.2345678998765400000000000000000
我有一個雙精度值,我想將其轉換為大於默認15位數字的字符串。
為什么? 15位數字后基本上將是垃圾。 您可以使用DoubleConverter
類獲取確切的值:
string exact = DoubleConverter.ToExactString(value);
...但是15位數字之后,其余的只是噪音。
如果您希望有意義的數據超過15個有效數字,請使用decimal
。
雙精度是不可能的,因為它不支持超過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
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.