Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# how to convert double as a currency

meera rao
Ranch Hand
Posts: 67
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

Marilyn de Queiroz
Sheriff
Posts: 9067
12
Are you using java 1.4 or 1.5? If you're using 1.5, you can use printf, otherwise I think NumberFormat is your best bet.

meera rao
Ranch Hand
Posts: 67
Thank you , I am using 1.4

marc weber
Sheriff
Posts: 11343
For currency values in 1.4, I would look at DecimalFormat (which is a subclass of NumberFormat in java.text).

http://java.sun.com/j2se/1.4.2/docs/api/java/text/DecimalFormat.html

Create an instance of DecimalFormat, passing a String pattern (probably "0.00") to the constructor. Then call the format method on your instance, passing your currency value as an argument.

(Note, however, that DecimalFormat uses the ROUND_HALF_EVEN mode, which might not be what you expect.)

Layne Lund
Ranch Hand
Posts: 3061
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.

Keep Coding!

Layne

marc weber
Sheriff
Posts: 11343
Originally posted by Layne Lund:
...If you are doing any arithmetic operations with these currency values you should consider using int or long instead of double...

That's a good, safe approach. Just be aware that int division always truncates.

Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
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.

yash Vi
Ranch Hand
Posts: 41
[<b>]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.
[</b>]

Why dont you use BigDecimal to avoid precision errors.

Barry Gaunt
Ranch Hand
Posts: 7729
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 ]