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 -

how to prompt save As Box in Excel Interlop c# MVC 4 -

xslt 1.0 - How to access or retrieve mets content of an item from another item? -