How to round up a number in Javascript?
Little late but, can create a reusable javascript function for this purpose:
// Arguments: number to round, number of decimal places
function roundNumber(rnum, rlength) {
var newnumber = Math.round(rnum * Math.pow(10, rlength)) / Math.pow(10, rlength);
return newnumber;
}
Call the function as
alert(roundNumber(192.168,2));
/**
* @param num The number to round
* @param precision The number of decimal places to preserve
*/
function roundUp(num, precision) {
precision = Math.pow(10, precision)
return Math.ceil(num * precision) / precision
}
roundUp(192.168, 1) //=> 192.2