heck, some DECIMAL fractions cannot be represented exactly in a finite space... 1/3 is NOT equal to 0.33333333333333, no matter how many "3"s you can store. 1/7 is NOT equal to 0.142857142857, but that's about the best your computer can do. since the computer has limited space, there will often be rounding.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Joined: Dec 28, 2006
Thanks for your explanation
I understand that numbers cannot be always exactly stored in computer memory. This causes that sometimes the ValueOf method of the String will return a strange value, because this method will return the exact double value in memory.
Originally posted by wilhelm tell: The solution for this is using the NumberFormat.
Just using NumberFormat does not suddenly make the value stored in the double variable more precise. It just rounds the number before it's printed on screen, so that you see the value that you expect. But this does not necessarily always work correctly - if the extra precision isn't there, then NumberFormat cannot magically create the missing information.
If you really need arbitrary precision (for example because you are working with money, in which rounding errors are not acceptable), you should use class BigDecimal instead of double.
Actually, if you are working with money, the commonly used practice is to use some kind of Integer, and work in pennies (or the equivilent of the currency you are working with), then format that appropriatly for display.
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