对小数进行四舍五入时会得到不同的结果

发布于 2024-11-08 13:04:50 字数 409 浏览 7 评论 0原文

double radius = 5;
double area = Math.PI * Math.pow(radius, 2);            
// System.out.println(area);

BigDecimal bd = new BigDecimal(area).setScale(2, HALF_UP);
// BigDecimal bd = new BigDecimal(area).setScale(2, ROUND_HALF_UP);
System.out.println(bd.toString());

上面的输出打印为 78.54,但如果我通过计算器(Windows calc 接口)执行相同的操作,输出结果为 78.57(即 22/7 * 25)。

为什么会出现不一致的情况

double radius = 5;
double area = Math.PI * Math.pow(radius, 2);            
// System.out.println(area);

BigDecimal bd = new BigDecimal(area).setScale(2, HALF_UP);
// BigDecimal bd = new BigDecimal(area).setScale(2, ROUND_HALF_UP);
System.out.println(bd.toString());

The above output gets printed as 78.54, but if I perform the same operation via the calculator (windows calc interface) the output comes out as 78.57 (i.e. 22/7 * 25).

Why is there an inconsistency

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

羁客 2024-11-15 13:04:50

您是否使用 22/7 作为 PI 的近似值?因为 22/73.142857142857...,其中 PI 约为 3.14159...。这解释了您的舍入不一致的情况。

JVM 中使用的 PI 近似值记录在 这里。根据JavaDoc,它是:

比任何其他值更接近 pidouble

Are you using 22/7 as an approximate value for PI? Because 22/7 is 3.142857142857... where PI is approximately 3.14159.... This explains your rounding inconsistencies.

The approximation of PI that is used in the JVM is documented here. According to the JavaDoc, it is:

The double value that is closer than any other to pi

回忆那么伤 2024-11-15 13:04:50

22/7PI 的近似值,它不准确,所以结果

22/7 is approximation to PI , Its not exact and so the result

禾厶谷欠 2024-11-15 13:04:50

几乎完全偏离主题(及其 javascript)。但这仍然是一个有趣的答案,因为可以实际看到 PI 的计算结果。

这种方法在计算 PI 时显然超级超级慢……但是看起来很有趣。

(function (t) {
    (function () {
        var a = 0,
            b = 1,
            d = 4,
            c = ~1e9;
        (function p() {
            if (!t) {
                return;
            }
            b -= 2;
            a -= d / b;
            d = -d;
            if (++c % 2000 === 0) {
                t.innerHTML = a;
                setTimeout(p, 13);
            } else {
                if (c) {
                    p();
                } else {
                    t.innerHTML = a;
                }
            }
        }());
    }());
}(document.getElementById("someplace-to-hold-the-calculation")))

Philippe Leybaert 设置了一个JSBin

Almost completely off-topic (and its javascript). But its still a fun answer for actually visibly seeing PI being calculated.

This method is obviously super-super-slow at calculating PI...but is kinda fun to watch.

(function (t) {
    (function () {
        var a = 0,
            b = 1,
            d = 4,
            c = ~1e9;
        (function p() {
            if (!t) {
                return;
            }
            b -= 2;
            a -= d / b;
            d = -d;
            if (++c % 2000 === 0) {
                t.innerHTML = a;
                setTimeout(p, 13);
            } else {
                if (c) {
                    p();
                } else {
                    t.innerHTML = a;
                }
            }
        }());
    }());
}(document.getElementById("someplace-to-hold-the-calculation")))

Philippe Leybaert set up a JSBin for it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文