【发布时间】:2016-05-16 13:37:13
【问题描述】:
public static double round(double number,int numberofdecimals){
int n=1;
for (int i = 1; i <= numberofdecimals;i++){
n=n*10;
}
double c =((number - Math.floor(number))*n);
c= Math.floor(c);
return (Math.floor(number) + (c/n));
}
我的方法完美地舍入了一些双精度,但是当我将 ((5.0 / 3) ,2) 放入我的数字参数时,它给了我 1.6600000 发生了什么?
【问题讨论】:
-
这仅仅是因为浮点数学被破坏了。看到这个 - stackoverflow.com/questions/588004/…
-
我编辑了方法 broj 是数字,但在我的语言中我忘记将其翻译成英文,但这不是问题
-
看起来问题只是由于在计算过程中丢失了精度。请参阅this post 了解更多信息。
-
Milos,请查看@Blue 发布的问题,看看它是否有足够的信息来帮助您解决问题。 (提示:应该)。
-
不确定您还能期待什么其他结果,因为您明确使用了
floor
标签: java floating-point rounding