Home > Net >  Add comma separator in react native numbers
Add comma separator in react native numbers

Time:12-12

I am building a react native application that involves dealing with numbers, my issue is I want a comma separator on the numbers i.e. instead of 1000 I want it to show 1,000. I am able to use .toLocaleString() and it works fine in development but when I build the app apk the .toLocaleString() method seems to be ignored. How do I go about this? Is there any better alternative?

CodePudding user response:

There's Intl.NumberFormat, but getting Intl to work in React Native can be a pain in the butt. so personally, I'd just do it myself using a regex, like so

function formatWithCommas(n) { 
  return n.toString().replace(/\B(?=(\d{3}) (?!\d))/g, ","); 
}
  • Related