0

I am using Jquery Knob and the input value is change through sliders.

window.onload = function(){
   var elements=document.querySelectorAll('input[type=range]')
   for(var i = 0; i < 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 i want to round the "buf" variable to 1 decimal, i tried to use toFixed and Math.round.... But i cant get it to work, can someone please help me?

EDIT:I now used this, if i use console.log it all 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 it show 1.7. however in the input field of Jquery Knob it shows 1.7000000000000002. See image below.

enter image description here Now how do i solve this problem

1 Answers1

0

I dont understand why .toFixed and Math.round is not working but we can think of an alternativeIf you are willing to process it like a string.

CheckThis

function RoundToOne(val)
{
var length =val.indexOf('.');
var newVal= val.substring(0,length+2);
return newVal;
}

http://jsfiddle.net/8XQen/1/

Dont forget to add validations

Edit

I dont think your issue is with .toFixed and Math.round its with your knob

Check These links about jQuery Knob

JQuery Knob display number change

I can't get jQuery knob to display a value

How to add a value suffix in jQuery.knob

Community
  • 1
  • 1
Amarnath R Shenoy
  • 5,121
  • 8
  • 24
  • 32