|
|
Re: Rounding error with double
|
Posted: Nov 22, 2004 3:37 AM
|
|
You can't.
Since computer floating data types are based on 2, not every number can be stored correctly. a double value is stored in the following way: a * b^c a is between 0 and 1, all variables are based on 2
You could use the data type Decimal, wich stores every decimal digit in an int-field. It uses much more memory and is lower, but is more precice for operations like this.
I you KNOW the result will be an int-value, you can allways use new Double (myValue).intValue() . This will round the value to 0 fraction digits.
But do you really NEED the exact value? If it's only for the outpur, you should write your own method to give for example only 3 digits (multiply the number per 1000, round it to int, convert it to String, insert the separator at the right place).
|
|