1

React Native does not format large numbers with "," as thousand separators when using toLocaleString('en-us'). The same problem is observed on both Android and iOS versions.

I am working on a simple calculator app for Android and iOS. I previously created a similar model for web (plain javaScript) and made it work. However, since I ported it to React Native, the resulting calculation will no longer have thousand separators. My React Native demo can be found here:

https://snack.expo.io/@leourushi/calculator-tolocalestring

An example of my calculation function:

text1: ( b/ ((c/100) + 1) ).toLocaleString('en-US')  

As you can see, I've added toLocaleString() method at the end of the calculation. But when the result is returned, it will not have "," as thousand separator.

Is toLocaleString() function not supported in React Native?

L.U.
  • 501
  • 5
  • 20

2 Answers2

0

Use Intl.NumberFormat instead. Have a look at link below, https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat

Omar
  • 463
  • 3
  • 11
  • Thanks. I tried that. And it gave me this error: "Can't find variable: Intl". Is this method supported by React Native? – L.U. Jun 23 '19 at 08:18
  • 1
    At the end, I resorted to this manual hack solution I found here: https://stackoverflow.com/questions/41408025/react-native-tolocalestring-not-working-on-android/45084376#45084376 . It's really not an ideal solution. But for my current need, this is good enough. number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") – L.U. Jun 23 '19 at 11:22
0

As I answered here https://stackoverflow.com/a/63750685/7124240 You can change the build flavor of JSC

Replace this line in your android/app/build.gradle file

def jscFlavor = 'org.webkit:android-jsc:+'

with this line

def jscFlavor = 'org.webkit:android-jsc-intl:+'