Can Babel make Number.prototype.toLocaleString() work alike in React Native on all phones?

Viewed 892

I'm using Number.prototype.toLocaleString() to get rounding and thousands separators working nicely with world currencies in my first React Native app.

It works great in the iOS but not on Android. I guess the different JS engines have differing support for toLocaleString.

I understand that BabelJS has support for polyfills but don't understand fully what it does and does not cover and cannot find information on this function in particular.

Is there a way I can get it to work in RN using Babl to polyfill it?

1 Answers

Looking up a polyfill for Number#toLocaleString(), I found this module, and it does not require babel to use. In order to activate it in your code, put this line somewhere before you need it :

// recommended
require('number-tolocalestring-polyfill')()

If you need to force the polyfill to load, even if it detects that the environment already supports Number#toLocaleString() you can do this:

// probably for debug purposes only
require('number-tolocalestring-polyfill')(true)
Related