public double hypotenuse()
{
//return Math.hypot(leg, leg); //returns 7.0710678118654755
//return 0.1 * Math.floor(Math.hypot(leg, leg) * 10); //returns 7.0
//return 0.1 * Math.ceil(Math.hypot(leg, leg) * 10); //returns 7.1000000000000005
return 0.1 * Math.round(Math.hypot(leg, leg) * 10); //returns 7.1000000000000005
}
我想四捨五入到最近的第十位,所以這個數字應該舍入到7.1。爲什麼此方法適用於Math.floor,但不適用於Math.round?有沒有人有任何見解?如何四捨五入到最接近的第十位
謝謝,
天空
什麼是這種語言? – 2015-03-13 19:43:26
不同的語言,但可能的點:http://stackoverflow.com/q/6875007/239394。在我看來,你只是遇到雙精度舍入問題。 – Andrew 2015-03-13 20:44:41
@EugeneSh。 Java的。 – 2015-03-13 22:07:16