Search code examples
androidreact-nativeintl

Intl.NumberFormat.formatToParts is not a function


I am using the Intl namespace in React Native to do some currency and metric amounts formatting (W.h, etc) When using the formatToParts method, everything works fine on IOS and also on Android debug mode. But in Android dev mode, I have the following error :

Intl.NumberFormat(_i18nJs.default.locale,options).formatToParts is not a function. (In 'Intl.NumberFormat(_i18n.Js.default.locale,options).formatToParts(value)','Intl.NumberFormat(_i18nJs.default.locale,options).formatToParts' is undefined)

In release mode, the screen on which I use this function makes the application crash

Code:

public static formatNumberWithCompacts(value: number, options: FormatNumberOptions = {}): FormatNumberResult {
    const isCompactForm =
      options.notation === NumberFormatNotationEnum.COMPACT &&
      (!options.compactThreshold || (options.compactThreshold && value > options.compactThreshold));
    const isCurrency = options.currency && options.style === NumberFormatStyleEnum.CURRENCY;
    options.currency = options.currency || I18nManager.currency;
    const isUnit = options.unit && options.style === NumberFormatStyleEnum.UNIT;
    const isPercent = options.style === NumberFormatStyleEnum.PERCENT;

    if (!isCompactForm) {
      delete options.notation;
    }
    // Format the given value with the given options
    const parts = Intl.NumberFormat(i18n.locale, options).formatToParts(value);

Solution

  • I fixed the issue by adding to my index.ts all the polyfill imports indicated here: https://github.com/web-ridge/react-native-paper-dates/releases/tag/v0.2.15

    I also had to install each of this polyfills beforehand as indicated in the doc of Format.js : https://formatjs.io/docs/polyfills