Round a double to 2 decimal places [duplicate]

2018-12-31 01:25发布

This question already has an answer here:

If the value is 200.3456, it should be formatted to 200.34. If it is 200, then it should be 200.00.

13条回答
浮光初槿花落
2楼-- · 2018-12-31 02:05

I think this is easier:

double time = 200.3456;
DecimalFormat df = new DecimalFormat("#.##");      
time = Double.valueOf(df.format(time));

System.out.println(time); // 200.35

Note that this will actually do the rounding for you, not just formatting.

查看更多
登录 后发表回答