javascript - How to format a number using jQuery? Ex. 119.0484 I want to format it to "9999.99"? -


how format number using jquery? ex. 119.0484 want format "9999.99"?

how can achieve this? can give sample code? thank can me this. i've tried code. whenever click radio button again value changing. think problem here structure of code. want know if .tofixed(2) solved 9999.99 format.. html

  <input type="radio" name="unitscale" id="opt1" onclick="changeunit()" checked class="roleauthorization"><label>metric (kg&cm)</label>   <input type="radio" name="unitscale" id="opt2" onclick="changeunit()" class="roleauthorization"><label>english (lbs&in)</label> 

script

function changeunit() {     var requestnum = $("#requestnum").val();     var username = $("#username").val();     if ($("#opt1").is(":checked")) {         $("#kg").removeattr("style");         $("#lbs").attr("style", "display:none;");         $("#cmtrs").removeattr("style");         $("#inches").attr("style", "display:none;");         converttometric();         } else if ($("#opt2").is(":checked")) {         $("#kg").attr("style", "display:none;");         $("#lbs").removeattr("style");         $("#cmtrs").attr("style", "display:none;");         $("#inches").removeattr("style");         converttoenglish();            } }  function converttometric(){     var weight = $("#weight").val();     var height = $("#height").val();     if ((weight != "")&&(height != "")){         weight = weight / 2.2046;         weight = weight.tofixed(2);         height = height / 0.39370;         height = height.tofixed(2);         $("#weight").val(weight);         $("#height").val(height);     } }  function converttoenglish(){     var weight = $("#weight").val();     var height = $("#height").val();     if ((weight != "")&&(height != "")){         weight = weight / 2.2046;         weight = weight.tofixed(2);         height = height / 0.39370;         height = height.tofixed(2);         $("#weight").val(weight);         $("#height").val(height);     } } 

your question not clear.

but seems looking fix decimal points. if ,you can check , use tofixed method.

// rounding var x = 119.0484; document.write('<pre>'+x.tofixed(2)+'</pre>')   // without rounding var val = (math.floor(100 * x) / 100).tofixed(2); document.write('<pre>'+val+'</pre>') 

jsfiddle


Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -