• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

how to convert double as a currency

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 9099
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you , I am using 1.4
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Mac Safari Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[<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.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Something must be done about this. Let's start by reading this tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!