提问人:Ackdari 提问时间:4/21/2020 更新时间:4/23/2020 访问量:1092
如何全精度打印浮点数
How to print floating point numbers in full precision
问:
如果我有一个值不能准确表示,或者我怎么能把它的实际值打印为字符串。像这样的程序:0.3
double
float
using System;
class Program
{
static void Main(string[] args)
{
var line = Console.ReadLine();
var d = float.Parse(line);
Console.WriteLine("{0:R}", d);
}
}
with as input do print 这对我来说很奇怪,因为根据这一点,它应该是这样的。0.3
0.3
0.300000011920928955078125
所以问题是我怎样才能强制.net格式化/作为它的真正价值。float
double
答:
1赞
Pavel Anikhouski
4/21/2020
#1
R
建议仅用于该类型。对于类型,请使用 “17国集团”;对于类型,请使用“G9”。BigInteger
Double
Single
因此,请尝试使用(因为为您提供最多 9 位有效数字的精度),它为您提供了预期的输出G9
float
Console.WriteLine("{0:G9}", d);
在我这边,我会看到价值0.300000012
评论
double