How to round 0.023 to 0.03 in JavaScript
This is what i tried. but it gives 0.024
var abc = 0.023, factor = 0.003;
abc = Math.round(abc / factor) * factor;
console.log(abc);
how should i get 0.03
How to round 0.023 to 0.03 in JavaScript
This is what i tried. but it gives 0.024
var abc = 0.023, factor = 0.003;
abc = Math.round(abc / factor) * factor;
console.log(abc);
how should i get 0.03
I think you mean rounding float number up. I found solution here: How to round up a number in Javascript?
First parameter is number that you want to round. Second parameter is number (integer) of numbers after point that you want to get.
function roundUp(num, precision) {
precision = Math.pow(10, precision)
return Math.ceil(num * precision) / precision
}
result = roundUp(0.023, 2)
console.log(result)
abc = Math.ceil((abc/factor)*factor * 100) / 100;
ceil rounds up a number to the integer value.