The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

I have built a simple page for a client that calculates a total from a bunch of items that the user enters on the page. The total is calculated by using the '+' operator. Most of the time, the addition results are correct; for instance, 1.50 + 2.50 results in 4. However, consider the following results: 1.19 + 6.45 should result in 7.64, but the result of the Javascript addition is 7.640000000000001. This is causing a problem on the page, because the client wants the total displayed in dollars and cents. In some cases, the addition results in a number such as 6.68999999999999995, and since Jscript's 'round' function rounds to the nearest integer, I lose some precision by calling 'round' on the total (the number above would be rounded to 7, which is wrong.)Simple addition on a calculator results in 6.69. I've checked the documentation for Microsoft Jscript, and ensured that the numbers I'm adding are indeed being treated as numbers, not strings etc. Why is the precision different for some addition operations? Am I missing something? Any suggestions would be much appreciated!

Thanks LuZeR. I got it to round correctly using that trick, but my frustration still lies with trying to understand why 1.19 plus 6.45 is 7.630000000000001 instead of 7.64 using the Jscript '+' operator. Oh well. Ours is not to question why, ours is but to do or die.