如何在java(Android)中舍入DecimalFormat

Med*_*edo 1 java android rounding

我想要回合

0.005 to 0.01
Run Code Online (Sandbox Code Playgroud)

即时通讯使用此代码

DecimalFormat df = new DecimalFormat("0.00");
Run Code Online (Sandbox Code Playgroud)

但是0.00 如果有可能,任何人都有一个想法,答案一直在我只想要右边的2位数进行舍入?

Lui*_*oza 9

您应该RoundingMode为DecimalFormat 添加

double d = 0.005;
DecimalFormat df = new DecimalFormat("0.00");
df.setRoundingMode(RoundingMode.HALF_UP);
System.out.println(df.format(d)); //0.01
Run Code Online (Sandbox Code Playgroud)