javascript - JS round to 2 decimal places -
this question has answer here:
- round @ 2 decimal places (only if necessary) 42 answers
i trying limit returned number 2 decimal places code isn't working me;
function myfunction() { var x = document.getelementbyid("myselect").value; document.getelementbyid("demo").innerhtml = "result is: " + x * 1.09; value = valtoround.tofixed(2);
}
what doing wrong?
typing in js browser console
x = 2.71828 x.tofixed(2) "2.72"
it clear .tofixed(2)
works
what did wrong rounding after printing answer, , not using correct variables.
document.getelementbyid("demo").innerhtml = "result is: " + x * 1.09; value = valtoround.tofixed(2);
it idea in habit of converting strings numbers parsefloat()
. in js, '2'*'2' '4' '2'+'2' '22', unless first convert number.
if way work:
function myfunction() { var x = parsefloat(document.getelementbyid("myselect").value); var valtoround = x * 1.09; var value = valtoround.tofixed(2); document.getelementbyid("demo").innerhtml = "result is: " + value; }
Comments
Post a Comment