You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
programming-examples/js/Math/Round a number to a given s...

14 lines
415 B
JavaScript

function precise_round(num, dec){
if ((typeof num !== 'number') || (typeof dec !== 'number'))
return false;
var num_sign = num >= 0 ? 1 : -1;
return (Math.round((num*Math.pow(10,dec))+(num_sign*0.0001))/Math.pow(10,dec)).toFixed(dec);
}
console.log(precise_round(12.375,2));
console.log(precise_round(12.37499,2));
console.log(precise_round(-10.3079499, 3));