I wish to format a number so that it has "leading zeros" (thus string is always equal length). - While I could as backup do this manually I wish to use the best option. - As a standard format syntax (like pythons "{0:4d}".format(number) ) seems to be unavailable, the next best is to use the internationalization library?
I've tried doing this in a fiddle: https://jsfiddle.net/f4202ne3/1/
let fmt = new Intl.NumberFormat('en-US',
{minimumIntegerDigits: 4,
maximumFractionDigits: 0
});
m = 1;
n = 5001
console.log(m.toString(), fmt.format(m)) //"0001"
console.log(n.toString(), fmt.format(n)) ///"5000"
However this displays "5,000" and "0,001". How do I set the thousands separator to a value ('')?
Simply set the useGrouping option to false
let fmt = new Intl.NumberFormat('en-US', {
minimumIntegerDigits: 4,
maximumFractionDigits: 0,
useGrouping: false
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With