Mob Dev
Mob Dev

Reputation: 934

How to add thousand separator in react native

Upvotes: 3

Views: 1065

Answers (1)

Mob Dev
Mob Dev

Reputation: 934

For this, I have found two solutions

  • Fixed the decimal value return balance.toFixed(2).replace(/\B(?=(\d{3})+(?!\d))/g, ",");

  • Spit the balance and after formatting rejoin the balance

`

function thousandFormatter(number) {
const THOUSAND_FORMATTER = new RegExp(/\B(?=(\d{3})+(?!\d))/g);
     if (!Boolean(number)) {
         return;
     }
     const numberSplitter = ".";
     if (number.includes(numberSplitter)) {
         const numberToFormat = number.split(numberSplitter);
         return [numberToFormat[0].replace(THOUSAND_FORMATTER, ","), numberToFormat[1]]
             .join(numberSplitter)
             .trim();
     }
     return number.replace(THOUSAND_FORMATTER, ",");
 }

Upvotes: 5

Related Questions