1

I am having a bit of trouble with a few math calculation in javascript. The goal of this calculation is to generate a value when the user clicks on a text field.

For example: 1 Kilogram costs 32 cents to ship to America and the user wants to find out what 10KG will cost him which is $3.20. For this I have the following piece of javascript code:

    function calculate(num) {
        var weight  = document.getElementById('weight'+num);
        var price   = document.getElementById('price'+num);
        if(num == undefined || num == '' || num.length <= 0 || isNaN(weight.value) || isNaN(price.value)) return false;

        if(num == 1) multiplyBy = 0.32;
        if(num == 2) multiplyBy = 0.14;
        if(num == 3) multiplyBy = 0.24;
        if(num == 4) multiplyBy = 0.53;

        var sum = parseInt(document.getElementById('weight'+num).value) * multiplyBy;
        if(isNaN(sum)) return false;
        price.value = sum;
    }

The above code works perfectly fine, however when I reverse the process (someone has $3.20 and wants to find out how much KG he/she can ship with that (which is 10KG) the script returns: 9.375KG

The following code is used for this calculation:

function reverse(num) {
        var weight  = document.getElementById('weight'+num);
        var price   = document.getElementById('price'+num);
        if(num == undefined || num == '' || num.length <= 0 || isNaN(weight.value) || isNaN(price.value)) return false;

        if(num == 1) divideBy = 0.32;
        if(num == 2) divideBy = 0.14;
        if(num == 3) divideBy = 0.24;
        if(num == 4) divideBy = 0.53;

        var sum = parseInt(document.getElementById('price'+num).value) / divideBy;
        if(isNaN(sum)) return false;
        weight.value = sum;
    }

I honestly don't grasp why it is failing, It would be much appreciated if someone could help me out with this.

5
  • 1
    This has less to do with floating point errors as it does parsing a float as an int Commented Feb 17, 2012 at 10:41
  • 2
    Perhaps also a lesson in why using integers (so 1c == 1, rather than 1c == 0.01) for currency calculations is often the way to go. Commented Feb 17, 2012 at 10:42
  • Read too fast. I'll remove it. Commented Feb 17, 2012 at 10:42
  • @duffymo you could have left it. It's good info to have, and will probably come up later for something like this, but not the direct cause for the calculation error Commented Feb 17, 2012 at 10:45
  • 1
    I'm far more disturbed by all those "magic numbers" in the code. I wouldn't rest until I'd expunged them. Commented Feb 17, 2012 at 11:59

1 Answer 1

4
var sum = parseInt(document.getElementById('price'+num).value) / divideBy;

You are forcing price into an integer before dividing it. So if price is 3.20, you are actually dividing 3 / 0.32, which is 9.375.

Don't force it into an integer.

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

Comments

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.