number = 1
converted = number.toString().padStart(3, '0')
console.log(converted)
// Expected output
" 001 "
function numberWithSpaces(x) {
return x.toString().replace(/B(?=(d{3})+(?!d))/g, " ");
}
var x = parseFloat('9.656');
x.toFixed(0); // returns 10
x.toFixed(2); // returns 9.66
x.toFixed(4); // returns 9.6560
x.toFixed(6);
//ES2020 adds support for this in Intl.NumberFormat Using notation as follows:
let formatter = Intl.NumberFormat('en', { notation: 'compact' });
// example 1
let million = formatter.format(1e6);
// example 2
let billion = formatter.format(1e9);
// print
console.log(million == '1M', billion == '1B');
Run code snippet
function m(n,d){x=(''+n).length,p=Math.pow,d=p(10,d)
x-=x%3
return Math.round(n*d/p(10,x))/d+" kMGTPE"[x/3]}