使用Java android中的Math.round方法舍入到小数点后6位
我正在使用
double i2 = value * 2.23694; i2 = (double)(Math.round(i2 * 100)) / 100;
用于舍入双打。 但它只向小数点后2位舍入。
我希望它是小数点后6位。
有没有办法使用Math.round
并有6位小数?
你正在向Integer
s投掷东西,这将破坏任何舍入。 要使用double
s,请使用小数点(即100.0
而不是100
)。 如果你想要6位小数,请像这样使用1000000.0
:
double i2 = value * 2.23694; i2 = Math.round(i2*1000000.0)/1000000.0;
但一般来说,我认为DecimalFormat是一个更优雅的解决方案(猜测你希望它只舍入以呈现它):
DecimalFormat f = new DecimalFormat("##.000000"); String formattedValue = f.format(i2);
如果您使用显示的值只使用以下方法舍入到6位数
double a = 12.345694895; String str = String.format("%.6f", a );
double value = 12.3464367843; double rounded =(double)Math.round(value * 1000000)/ 1000000;
输出:12.346437