使用Java android中的Math.round方法舍入到小数点后6位

use*_*904 0 java android decimal rounding

我正在使用

double i2 = value * 2.23694;
i2 = (double)(Math.round(i2 * 100)) / 100;
Run Code Online (Sandbox Code Playgroud)

用于舍入双打.但它只到小数点后两位.

我希望它是小数点后6位.

有没有办法使用Math.round并有6个小数位?

ddm*_*mps 12

你正在把东西Integer扔到s上,这会毁掉任何四舍五入.要使用doubles,请使用小数点(即100.0代替100).如果你想要6位小数,请使用1000000.0如下:

 double i2 = value * 2.23694; 
 i2 = Math.round(i2*1000000.0)/1000000.0;
Run Code Online (Sandbox Code Playgroud)

但一般来说,我认为DecimalFormat是一个更优雅的解决方案(猜测你希望它只舍入以呈现它):

DecimalFormat f = new DecimalFormat("##.000000");
String formattedValue = f.format(i2);
Run Code Online (Sandbox Code Playgroud)


Ris*_*han 5

如果您使用的值显示只是使用下面的方法四舍五入到 6 位

double a = 12.345694895;
String str = String.format("%.6f", a );
Run Code Online (Sandbox Code Playgroud)