我如何将双精度取整到三分之二? [英] How do I round a double to the thirds place?

查看:101
本文介绍了我如何将双精度取整到三分之二?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

double1 = .05100000000003;

double2 = .05295;

如何将小数点四舍五入?我希望输出分别执行0.051和0.053。

How do I round a decimal to three places? Respectively, I'd like the output to do this 0.051 and 0.053.

我尝试过Math.round(double1);但我又回到了0.0。

I tried Math.round(double1); but I got back 0.0.

任何想法都会非常感谢。

Any ideas would be greatly appreciated thanks.

推荐答案

在Java中尝试以下代码。零数字表示您要舍入到小数点后位数。 (在这种情况下,有3个零)。

Try the following code in Java. The number of zeros indicates how many decimal places you would like to round to. (In this case there are 3 zeros).

(double)Math.round(value * 1000d) / 1000d

编辑:之前的StackOverflow帖子

这篇关于我如何将双精度取整到三分之二?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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