Reputation: 934
balance.toLocalString
but it's not working in AndroidUpvotes: 3
Views: 1065
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