[英]double.ToString() return wrong value in C#
I have double variable and its value is :我有双变量,它的值是:
double d = 0.000000000000056843418860808015;
when i print this variable its print wrong.当我打印这个变量时,它的打印错误。
d.ToString();
Output : "5.6843418860808E-14"
输出:“5.6843418860808E-14”
How to resolve this?如何解决这个问题?
Well if you want to have an output without the exponential notation you need to format your string:好吧,如果你想要一个没有指数符号的输出,你需要格式化你的字符串:
d.toString("F25");
This will give you the "correct" number with up to 25 fractional digits.这将为您提供最多 25 个小数位的“正确”数字。
0,0000000000000568434188608 0,0000000000000568434188608
Edit : Complete list of formats and conversions are available here and snapshot for you is below.编辑: 此处提供了完整的格式和转换列表,下面为您提供了快照。
Original value: 1054.32179
F: 1054.32
F0: 1054
F1: 1054.3
F2: 1054.32
F3: 1054.322
double d = 0.000000000000056843418860808015;
var str = d.ToString("G17");
var value = str.Split('E')[0];
var zeros = Int32.Parse(str.Split('E')[1]);
var outputString = "";
zeros = Math.Abs(zeros);
var addstring = "0.";
for (var i = 0; i < zeros - 1; i++)
{
addstring += '0';
}
value = value.Remove(1, 1);
value = addstring + value;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.