javascript - Round integer number to 1 decimal -
i using jquery knob , input value change through sliders.
window.onload = function(){ var elements=document.queryselectorall('input[type=range]') for(var = 0; < elements.length; i++){ elements[i].addeventlistener('change',calcul , false); } } function calcul(){ var elements = document.queryselectorall('input[type="range"]') var len = 0 var buf = 0 for(var i=0;i<elements.length;i++) { if(elements[i].parentnode.parentnode.style.display != 'none'){ buf += parseint(elements[i].value) len++ } } buf = len === 0 ? 0 : buf/len document.getelementbyid("knob").value=buf; $("#knob").trigger("change"); }
now want round "buf" variable 1 decimal, tried use tofixed , math.round.... cant work, can please me?
edit:i used this, if use console.log goes well.
buf = len === 0 ? 0 : buf/len; gem_cijfer = buf.tofixed(1); document.getelementbyid("knob").value=gem_cijfer; document.getelementbyid("form_knob").value=gem_cijfer; $("#knob").trigger("change");
example problem, in console.log show 1.7. in input field of jquery knob shows 1.7000000000000002. see image below.
how solve problem
i dont understand why .tofixed
, math.round
not working can think of alternativeif willing process string.
checkthis
function roundtoone(val) { var length =val.indexof('.'); var newval= val.substring(0,length+2); return newval; }
dont forget add validations
edit
i dont think issue .tofixed
, math.round
knob
check these links jquery knob
jquery knob display number change
Comments
Post a Comment