Javascript treats everything as strings, unless you tell it that it is a number. (pain in the neck, mind you!)
So:
var p1 = parseInt ("1");
var p2 = parseFloat ("1,5");
var p3 = Number ("2");
var tax = parseFloat (( p1*25 ) + ( p2 * 65 ) + ( p3 * 15) );
document.write("Your total without tax is: " + tax );
You can also force a string (or even a function) to be converted into number using "eval".
Try this:
document.write("Your total without tax is: " + (p1*25 + p2*65 + p3*15));
Hi, I'm doing a JavaScript project where I have to add variables together to get a total and when I'm using the + sign they are stringing together and not adding like I need them to. How do I get these variables to add normally rather than string?
CODE:
document.write("Your total without tax is: " + p1*25 + p2*65 + p3*15);
So the user inputs a value for p1, p2, & p3 and I need to add them in the end ( to test it I used 1 for each variable. It is coming out as > 256515
Thank You