Originally posted by meera rao: I am storing all the currency values as double. When i calculate 2 double numbers, I don't get an output with 2 decimal places.
Can somebody help me convert it into 2 decimal places.
I know one option using NumberFormat. Are they any other ways of doing this
If you are doing any arithmetic operations with these currency values you should consider using int or long instead of double. Assuming you are using U.S. currency, the int (or long) will store the number of pennies (instead of the number of dollars as you do in a double). You will then format the value as necessary for output. This approach avoids common rounding errors and lack of precision that is introduced by storing numbers as a floating-point value.
See the thread about why use BigInteger. There is an exercise that compares calculations with Double and BigInt. If it was my bank account, I'd say one answer was correct and the other was not, depending on whether it was crediting or debiting the extra 4 billionths of a cent.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
If you do decide to use java.math.BigDecimal do not use the BigDecimal(Double) constructor - use the BigDecimal(String) one. Otherwise, BigDecimal(0.10) will still not get you what you want. [ July 24, 2005: Message edited by: Barry Gaunt ]