aspose file tools*
The moose likes Java in General and the fly likes double or float Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "double or float" Watch "double or float" New topic
Author

double or float

DANIELA PINES
Greenhorn

Joined: Jun 25, 2003
Posts: 8
What would be the best practice for currency computations? Use data type double, float, Double or Float?
Thanks.
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Originally posted by DANIELA PINES:
What would be the best practice for currency computations? Use data type double, float, Double or Float?
Thanks.

Well it really depends on the magnitudes. If you want to calculate the US national debt to the exact cent then you would probably use a java.math.BigDecimal which provides for arbitrary precision. For normal calculations doubles should do fine.


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31054
    
232

For calculations, doubles are fine. But be careful if you need to compare two doubles. 1.00001 is not equal to 1.00 and you can get some precision differences from the computation due to the way doubles are stored.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Philip Shanks
Ranch Hand

Joined: Oct 15, 2002
Posts: 189
Originally posted by Jeanne Boyarsky:
For calculations, doubles are fine. But be careful if you need to compare two doubles. 1.00001 is not equal to 1.00 and you can get some precision differences from the computation due to the way doubles are stored.

To expand on what Jeanne said, if you need to compare two numbers, you are better off comparing the difference to some tolerance factor, as
if ( (d2 - d1) < 0.00001 ) ....
And since part of your question has to do with double vs. Double, you are talking about a Java primitive type vs. a wrapper class around a primitive. The underlying behavior is the same.


Philip Shanks, SCJP - Castro Valley, CA
My boss never outsources or has lay-offs, and He's always hiring. I work for Jesus! Prepare your resume!
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Jeanne Boyarsky:
For calculations, doubles are fine.

Not when dealing with big amounts. See the current newsletter: http://www.javaranch.com/newsletter/July2003/newsletterjuly2003.jsp#a4


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31054
    
232

That article explains why you should not use floats for money. It doesn't mention the use of doubles.
I imagine that doubles would have a similar effect for really large numbers. However, I work with numbers in the billions/trillions and have not encountered any problems in calcuations.
Trond Hundstuen
Greenhorn

Joined: Jul 10, 2003
Posts: 3
Actually you shouldn't use float or double. A far better choice for monetary calculations is the BigDecimal class. The BigDecimal class is also what you should use if you want to round off.
Best regards,
Trond
Gaurav Mac Mathur
Ranch Hand

Joined: Feb 19, 2002
Posts: 47
Would further recommend to write your Own Currency class and store value in BigDecimal, and have your formatter methods and mathamatical operators in that class.
Like :

Regards
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: double or float