代码如下:
static void Main(string[] args)
{
int xd2 = 5;
for (double xd = (double)xd2; xd <= 6; xd += 0.01)
{
Console.WriteLine(xd);
}
}
这是输出:
我想继续加 0.01(正如您在屏幕上看到的,有时恰好加 0.99999) 谢谢
最佳答案
使用decimal
如果你想保持这种准确性。
浮点类型不能准确表示某些值。我建议阅读 What Every Computer Scientist Should Know About Floating-Point Arithmetic以获得全面的解释。
decimal xd2 = 5m;
for (decimal xd = xd2; xd <= 6m; xd += 0.01m)
{
Console.WriteLine(xd);
}
关于c# - 如何使 double 正常工作? C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10693098/