aspose file tools*
The moose likes Beginning Java and the fly likes how to convert double as a currency Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to convert double as a currency" Watch "how to convert double as a currency" New topic
Author

how to convert double as a currency

meera rao
Ranch Hand

Joined: Jun 30, 2005
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

Joined: Jul 22, 2000
Posts: 9044
    
  10
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.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
meera rao
Ranch Hand

Joined: Jun 30, 2005
Posts: 67
Thank you , I am using 1.4
marc weber
Sheriff

Joined: Aug 31, 2004
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.)


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
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


Java API Documentation
The Java Tutorial
marc weber
Sheriff

Joined: Aug 31, 2004
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

Joined: Jan 29, 2003
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.


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
yash Vi
Ranch Hand

Joined: Jul 17, 2005
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.


Thanks for your reply and time.<br />Windows 2000,j2sdk1.4.2_08
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
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 ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to convert double as a currency