0

I hate to add to the litany of questions regarding this subject but I dont know where else to turn for a concise answer so here goes:

I have this form on jsFiddle

I got to this point by combining several different examples I have found online today to get the overall effect I wanted.

While researching I found a lot of posts talking about the hazards of adding floats. I believe I understand the concept of why this is normally a bad idea regarding precision. However, its seems like this is only really an issue when the sum of the two numbers is given with several decimal places. (like 6 or more)

In my code i will only ever add numbers that have 2 decimal places and I use var n=val.toFixed(2); to make sure the result has 2 and only 2 decimal places. When testing everything seems to work great.

My question is twofold:

  1. Is this an acceptable way to achieve my desired result and maintain precision given that I only need 2 decimal places?

  2. Can you point me to an example that shows a better or more accepted way to do what I want?

With all due respect (and I owe this community a lot of respect) I know that "client side validation does not replace server side validation" and I only need to support Chrome so if anything in my code doesn't work as expected in any other browser that's not an issue.

1 Answer 1

1

In general, yes. Floats can usualy offer 15 significant digits of precision, so keeping only 2 digits will almost always work.

In my personal experience, however, I find it is more relible to always work in integers - even going so far as to use string operations to remove the decimal point and keep track of how many powers of ten I effectively multiplied the original number by so I can divide again at the end.

Sign up to request clarification or add additional context in comments.

6 Comments

That sounds reasonable, So if I understand what your saying instead of doing 112.23 + 112.24 = 224.47 I should do (112.23 * 100) + (112.24 * 100) = 22447 Then do 22447 / 100 = 224.47 and set that to my "results box" correct?
Hmm, seems you really meant to just remove the decimal, add the two integers, then devide by 100 (in my case) Im gonna work on that, Thank you very much for your help pointing me in the right direction
@DelightedD0D I would also recommend to use a | 0 or >> 0 after multiplying to ensure you have an Integer. => ((112.23 * 100 | 0) + (112.24 * 100 | 0)) / 100
@pvorb hmm, Im not familiar with using "|" in this way. What exactly is this doing?
It is a bitwise OR operator. That means every bit of the integer on the left OR the corresponding bit of the integer on the right (or both) have to be 1 to make that bit in the result 1 (e.g. 0100 | 0110 = 0110 with binary integers). So if you have 0 on the right side of this operator, it basically doesn't change the integer on the left. But to apply the number on the left to this operator, it has to be an integer and JavaScript does that conversion implicitly.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.