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
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.
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!
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
author & internet detective
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.
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