0

I can't get the var total to resolve by using addition. It will work with multiplication operator: var total = subtotal * sales_tax but not with the + sign addition operator: var total = subtotal + sales_tax. Any help would be most appreciated.

var calculate_click = function () { 
    var subtotal = parseFloat(document.getElementById("subtotal").value).toFixed(3); 
    var taxRate  = parseFloat(document.getElementById("tax_rate").value).toFixed(3);  

    if (isNaN(subtotal) || isNaN(taxRate)) {    
    } 
        else {                                  
        var sales_tax = (subtotal * taxRate / 100); 
        parseFloat(document.getElementById("sales_tax").value = sales_tax.toFixed(3));  

        var total = subtotal + sales_tax;   
        parseFloat(document.getElementById("total").value = total.toFixed(3));  
    }
}
5
  • When you say that it doesn't work, what's the output? Can you link to a running example (JSFiddle or StackOverflow's new code editor)? Commented Oct 2, 2014 at 19:08
  • Also the calls to parseFloat() around the code where you set the field values are pointless. Commented Oct 2, 2014 at 19:08
  • Why you put input values assign into parseFloat? Commented Oct 2, 2014 at 19:09
  • Your indentation is a little misleading. It makes it look like the else is inside the if block. Commented Oct 2, 2014 at 19:09
  • @EricNguyen Well, it works in my browser, but apparently not in JSFiddle. The result is simply a blank space when trying to get a total. jsfiddle.net/Ronando/m9jLt0L2 Commented Oct 2, 2014 at 19:15

1 Answer 1

5

toFixed() formats the number into a string. So arithmetic operations afterwards will not work as expected.

Note:

  • + (concatenation) is a valid operation for strings as well, so it'll return "string1string2" - For all other arithmetic operations it auto converts the strings to numbers and performs the operation. If the data within the strings cannot be converted, it returns NaN.
  • "12" + "2" => "122" whereas "12" * "2" => 24 (number) and "hello" * "3" => NaN
Sign up to request clarification or add additional context in comments.

5 Comments

but the string will work with a multiplication operator? That's what confused me, the the formula worked with multiplication but not addition.
Thats because + (concatenation) is a valid operation for strings as well, so it'll return "string1string2" - For all other arithmetic operations it will auto convert the strings to numbers and perform the operation (when possible, otherwise it returns NaN).
"12" + "2" gives "122" whereas "12" * "2" gives 24 and "hello" * "3" gives NaN
I see! That makes sense. Thanks. Previously I thought it might have to do with being a string, so I tried parsing it, but I still couldn't get it to work. Can you help in getting it to work? Also, I seem to have trouble in understanding parsing since others say I don't need it on the subtotal and taxRate variables. When I remove the parse, it doesn't work due to the parentheses (which I can't find where to put). var subtotal = parseFloat(document.getElementById("subtotal").value).toFixed(3);
Yes! That was perfect! I should have known it was something that simple. Thank you. This is very helpful and allowed me to complete my homework after 6 hours of slamming my head against the keyboard.

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.