I am trying this code, but i am getting NaN
a = unidade.val();
b = unitario.val();
//alert(a);5
//alert(b);50,00
$(total).val(a * b); //NaN
Why? because is a int*float?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You have to parse the strings before you multiply as val always returns a string and
"50,00"can’t be converted automatically to a number.parseFloat("50,1")gives you50. If the comma here is the decimal separator, you have to replace it with a dot.So you probably need
EDIT :
if you want to allow numbers formatted like
2.500,00, then I propose this code :But it’s dangereous if you have users who prefer (or expect) the American notation. I’d probably stick to the American notation and show an error if the field contains a comma.
Note that HTML5 proposes
<input type=number>which forces the user to type number and let you get numbers directly. See reference.