numeric step values

Convert values to whole incremental steps, for example 1 through 100 by 5's.

by bob

HTML

<div id="output"></div>

JavaScript

function getStepValueInt (val, step) {
	return (Math.round(val / step) * step);
}

function getStepValueFloat (val, step) {
  var stepup = step * 100;
	return (Math.round((val * 100) / stepup) * stepup) / 100;
}

var divout = document.getElementById("output");
function printit(val){
    divout.innerHTML += val + "<br />";
}


printit("----- Int Steps @ 5 ---");
for(var i=0; i<20; i++){
	printit( i + " : " + getStepValueInt (i, 5) );
}

printit("----- Int Steps @ 10 ---");
for(var i=0; i<20; i++){
	printit( i + " : " + getStepValueInt (i, 10) );
}

printit("----- Float Steps ---");
for(var i=1; i<20; i++){
	var frac = i * 0.1;
    printit( frac.toFixed(2) + " : " + getStepValueFloat (frac, 0.25) );
}