开发者

how to round off a double value to the nearest 0.05 in java

开发者 https://www.devze.com 2023-01-20 04:59 出处:网络
i want to round off a double value to its nearest 0开发者_JS百科.05 in java. eg: 54.625 to 54.65 or

i want to round off a double value to its nearest 0开发者_JS百科.05 in java. eg: 54.625 to 54.65

or

32.1885 to 32.19 etc.


double foo = 54.625;
foo = (int)(foo * 20.0 + 0.5) / 20.0;

This is quick and dirty - it doesn't handle negative numbers correctly, for instance. But for simple problems, it's a simple solution.

0

精彩评论

暂无评论...
验证码 换一张
取 消