javascript - JS round to 2 decimal places -


this question has answer here:

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

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -