lui
lui

Reputation: 37

Add comma separator in react native numbers

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?

Upvotes: 0

Views: 919

Answers (1)

Nitsan BenHanoch
Nitsan BenHanoch

Reputation: 709

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})+\b)/g, ","); 
}

Upvotes: 2

Related Questions