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.
精彩评论