开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜