This week's book giveaway is in the Design forum. We're giving away four copies of Building Microservices and have Sam Newman on-line! See this thread for details.

Hi, I need to round some numbers to 2 decimal points. Since Math.round only rounds to nearest integer, I tried this approach: I multiplied the number by 100, rounded and then divided by 100 again. For example, to round .125 to 2 decimal points, I do this float oldVal = 0.125; float roundedVal = Math.round(oldVal * 100)/100; This seems to work for most numbers. But when the 3rd decimal point has a 5, e.g., .125 and .175, I seem to be getting inconsistent answers. .125 is rounded to .13 (rounded up), but .175 becomes .17 (rounded down). Can anyone sugeggest a way to get a consistent answer ? Thanks, Sankar

The machine-representation of a float or double isn't that, what you see. Therefore you shouldn't use them, when precision is needed. I guess BigDecimal is the proper class for these tasks.

Sankar, Just as we have trouble representing 1/3 in base 10, computers have problems representing 0.1 in binary. Convert your two numbers to their binary representation, and it will become clear why they are rounding the way you observed.

Tom Blough<br /> <blockquote><font size="1" face="Verdana, Arial">quote:</font><hr>Cum catapultae proscriptae erunt tum soli proscripti catapultas habebunt.<hr></blockquote>

I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com