need solution: 30.01 + 30.03= 60.0400000000000006

Discussions

EJB design: need solution: 30.01 + 30.03= 60.0400000000000006

1. need solution: 30.01 + 30.03= 60.0400000000000006 (5 messages)

hi all, pls advice any solution to the following overflow problem while adding 2 numbers in javascript. 30.01 + 30.03 = 60.0400000000000006 whereas 30.01 + 30.04= 60.05 thanks
2. Re: need solution: 30.01 + 30.03= 60.0400000000000006[ Go to top ]

In Java you would solve this by using a double instead of a float. In JavaScript you cannot make that choice so it is probably easiest to use cent values in your scripts and divide by 100 to present values. S.
3. Re: need solution: 30.01 + 30.03= 60.0400000000000006[ Go to top ]

If you are dealing with kind of accounting stuff, you should use BigDecimal class. Mithat
4. Re: need solution: 30.01 + 30.03= 60.0400000000000006[ Go to top ]

Any particular reason why a javascript error is posted in the ejb design forum?
5. Re: need solution: 30.01 + 30.03= 60.0400000000000006[ Go to top ]

You can always use something like this: function formatNumber(number) { return Math.round (number *100) /100; }
6. Re: need solution: 30.01 + 30.03= 60.0400000000000006[ Go to top ]

Its mainly because of inability to represent 0.1 exactly in binary. The problem is not with java,same result will be observed in c,c++,basic,pascal,etc..... Regards, Sanket