programming-examples/js/Math/Format a number up to specified decimal places.js
2019-11-15 12:59:38 +01:00

9 lines
328 B
JavaScript

function decimals(n, d) {
if ((typeof n !== 'number') || (typeof d !== 'number'))
return false;
n = parseFloat(n) || 0;
return n.toFixed(d);
}
console.log(decimals(2.100212, 2));
console.log(decimals(2.100212, 3));
console.log(decimals(2100, 2));