1. 程式人生 > >[React Intl] Format Numbers with Separators and Currency Symbols using react-intl FormattedNumber

[React Intl] Format Numbers with Separators and Currency Symbols using react-intl FormattedNumber

lan pass prop val div data tor display nbsp

Using a react-intl FormattedNumber component, we‘ll pass a Number and a few additional props in order to render the correct separator and currency symbols for different languages.

For example we have price data as such:

        "price": {
          "en-US": "16.19",
          "es-ES": "15.09",
          "fr-FR": "15.09"
        },

We can use FormattedNumber to display the correct currency.

let locale = (navigator.languages && navigator.languages[0])
             || navigator.language
             || navigator.userLanguage
             || ‘en-US‘;

            <p>
              <FormattedNumber 
                
style=‘currency‘ currency={locale === ‘en-US‘ ? ‘USD‘: ‘EUR‘} currencyDisplay=‘symbol‘ value={merchant.price[locale]} /> </p>

[React Intl] Format Numbers with Separators and Currency Symbols using react-intl FormattedNumber