tag:blogger.com,1999:blog-8712770457197348465.post5306889638605676285..comments2016-06-26T20:05:13.832-07:00Comments on Javarevisited: Java Mistake 1 - Using float and double for monetary or financial calculationJavin Paulhttps://plus.google.com/114528699166048052030noreply@blogger.comBlogger13125tag:blogger.com,1999:blog-8712770457197348465.post-83064862089976575572014-09-23T00:28:18.637-07:002014-09-23T00:28:18.637-07:00Decimal
In case of financial applications it is ...Decimal<br /> <br />In case of financial applications it is better to use Decimal types because it gives you a high level of accuracy and easy to avoid rounding errors<br /><br />Double<br /> <br />Double Types are probably the most normally used data type for real values, except handling money.<br /><br />Float<br /> <br />It is used mostly in graphic libraries because very high demands for lingmaakihttp://www.blogger.com/profile/09477565569807599471noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-80512921636461048032014-07-16T05:30:28.932-07:002014-07-16T05:30:28.932-07:00By Jelle Foks,
The keyword for financial calculati...By Jelle Foks,<br />The keyword for financial calculations, is not "arbitrary precision math", it is decimal fixed or floating point. The difference is in the rounding. There is a reason why it's defined as part of IEEE 754-2008, and if you don't use it where you should, people can end up with other people's money, that's why people tend to find it more important to do Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-38651980982491311052014-07-16T05:28:19.163-07:002014-07-16T05:28:19.163-07:00It is not a myth. Manually rounding on doubles is ...It is not a myth. Manually rounding on doubles is the same a applying duct tape: it will usually work, for a while... Its better to use a type/library that can represent your financial amounts exactly, with the exact/correct decimal rounding applied automatically after every operation.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-22847036643593136242014-07-15T08:28:05.599-07:002014-07-15T08:28:05.599-07:00This is nonsense and a damn myth. Double is fine f...This is nonsense and a damn myth. Double is fine for financial calculations, given you understand its limitations, and only want cent precision. <br /><br />The first important part to understand is that you can't use the == operator to determine equality for doubles (you shouldn't ever do this). <br /><br />Instead, you use an operator like <br />boolean equalsWithinPrecision(double a, Viktor Lofgrenhttp://www.blogger.com/profile/13511366674265946174noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-34261020804710123932014-01-26T17:34:22.884-08:002014-01-26T17:34:22.884-08:00You shouldn't use BigDecimal for financial cal...You shouldn't use BigDecimal for financial calculations either. You should really use a specific Currency class instead. Imagine this: You calculate a discount of 50% on a price of $5.55. That results in a discount of $2.755 and a discounted price of $2.755. Since we cannot pay $2.755 we need to round the numbers. So we round them. Result: Discount is now $2.76 and price is now $2.76 . Total Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-1530781293142551602014-01-21T12:34:47.766-08:002014-01-21T12:34:47.766-08:00This is not specific to Java: I learned that hard ...This is not specific to Java: I learned that hard way while developing Clipper application. Old mainframes had BCD numbers for the very same reason.Željko Trogrlićhttp://www.blogger.com/profile/05942594049791118602noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-80756144821172567302014-01-20T19:38:20.334-08:002014-01-20T19:38:20.334-08:00float and double data types are mainly provided fo...float and double data types are mainly provided for scientific and engineering calculations. Java uses binary floating point calculations which is good for approximation but doesn't provide exact result. Bottom line is, don't use float/double when exact calculation is needed. You can not represent values like 0.1 or 0.01 or any negative power of 10 accurately in Java. calculating interestAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-70790116288317466452013-05-08T05:30:36.574-07:002013-05-08T05:30:36.574-07:00@h143570, floating point calculation is a big topi...@h143570, floating point calculation is a big topic in itself. Double or float can not be used whenever you need exact numbers. By the way, thanks for sharing those links.Javin @ ClassLoader in Javahttp://javarevisited.blogspot.com/2012/12/how-classloader-works-in-java.htmlnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-31169199923463168362013-05-06T20:02:14.300-07:002013-05-06T20:02:14.300-07:00Using float and double for financial calculations ...Using float and double for financial calculations can be serious mistake. Great to see that you are educating people with this kind of practical advice, long is also not a perfect solution, as monetary calculation for notional and future predictions may go beyond range of long data type.Millernoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-9657360070787593282013-01-22T10:23:59.434-08:002013-01-22T10:23:59.434-08:00can anyone multiply (200 digit number) with (200 d...can anyone multiply (200 digit number) with (200 digit number) without using BIGNUMBER?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-58276825020265921232012-11-28T14:14:16.731-08:002012-11-28T14:14:16.731-08:00Funny to read about financial calculation. This ja...Funny to read about financial calculation. This java double format is useless for science too. Or want someone used that to calculate statistics of medical drugs safety for example? <br /><br />I think this type actually is mistake. Seams that someone forgot to set all bits from 8 byte number to 0 and rounds some memory garbage from a tail in every operation. And the most evil you can not predictAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-11231688229062520072012-03-03T19:33:33.461-08:002012-03-03T19:33:33.461-08:00Hi Javin ,
great post! learnt about why to use Big...Hi Javin ,<br />great post! learnt about why to use BigDecimal. <br />Looking forward for this article series.anotherjavadukehttp://anotherjavaduke.wordpress.com/noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-39429113510048921212012-02-29T12:42:05.395-08:002012-02-29T12:42:05.395-08:00Hey - that explain why all financial operations ar...Hey - that explain why all financial operations are computed using BigDecimal in my company.<br /><br />You opened my eyes!Anonymousnoreply@blogger.com