C#舍入的方式取决于平台? [英] C# rounding differently depending on platform?

查看:57
本文介绍了C#舍入的方式取决于平台?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这小段代码

double s = -2.6114289999999998;
double s7 = Math.Round(s, 7);
double s5 = Math.Round(s, 5);
double s6 = Math.Round(s, 6);

在平台=任何CPU的情况下,我得到

With Platform = Any CPU, I get

s7: -2.611429   
s5: -2.61143    
s6: -2.611429   

使用Platform = x64,我得到

With Platform = x64, I get

s7: -2.6114289999999998 
s5: -2.61143    
s6: -2.6114289999999998

为什么? (输出从VS的Locals窗口复制)

Why? (Output copied from VS's Locals window)

整段代码为:

    private void btnAlign_Click(object sender, EventArgs e)
    {
        double s = -2.6114289999999998;
        double s7 = Math.Round(s, 7);
        double s5 = Math.Round(s, 5);
        double s6 = Math.Round(s, 6);
    }


推荐答案

-2.611429 不能使用64位浮点表示。在32位模式下编译时,该值将改为使用80位(扩展精度)。

The value -2.611429 cannot be represented using 64-bit floating point. When compiling in 32-bit mode the value will instead use 80-bit (extended precision).

这篇关于C#舍入的方式取决于平台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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