如何使双打正常工作? C# [英] How to make doubles work properly? c#

查看:111
本文介绍了如何使双打正常工作? C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是代码:

    static void Main(string[] args)
    { 
        int xd2 = 5;

        for (double xd = (double)xd2; xd <= 6; xd += 0.01)
        {
            Console.WriteLine(xd);
        }

    }

这是输出:

and here's the output:

我想继续添加0.01(如您在屏幕上看到的那样,有时恰好添加0.99999)
谢谢

I want to keep on adding 0.01 (as You can see on the screen, sometimes it happens to add 0.99999) Thanks

推荐答案

如果需要,使用 十进制 为了保持这种准确性。

Use decimal if you want to keep this kind of accuracy.

浮点类型不能准确表示某些值。我建议阅读每个计算机科学家应该了解的有关浮点算法的内容

Floating point types cannot accurately represent certain values. I suggest reading What Every Computer Scientist Should Know About Floating-Point Arithmetic for a comprehensive explanation.

decimal xd2 = 5m;

for (decimal xd = xd2; xd <= 6m; xd += 0.01m)
{
    Console.WriteLine(xd);
}

这篇关于如何使双打正常工作? C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆