Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Number formatting, remove thousands separator

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 ('')?

like image 893
paul23 Avatar asked Mar 23 '26 07:03

paul23


1 Answers

Simply set the useGrouping option to false

let fmt = new Intl.NumberFormat('en-US', {
  minimumIntegerDigits: 4,
  maximumFractionDigits: 0,
  useGrouping: false
});
like image 196
T Porter Avatar answered Mar 25 '26 22:03

T Porter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!