globalization.getCurrencyPattern

Returns a pattern string to format and parse currency values according to the client's user preferences and ISO 4217 currency code.

 navigator.globalization.getCurrencyPattern(currencyCode, successCallback, errorCallback);

Description

Returns the pattern to the successCallback with a properties object as a parameter. That object should contain the following properties:

  • pattern: The currency pattern to format and parse currency values. The patterns follow Unicode Technical Standard #35. (String)

  • code: The ISO 4217 currency code for the pattern. (String)

  • fraction: The number of fractional digits to use when parsing and formatting currency. (Number)

  • rounding: The rounding increment to use when parsing and formatting. (Number)

  • decimal: The decimal symbol to use for parsing and formatting. (String)

  • grouping: The grouping symbol to use for parsing and formatting. (String)

The inbound currencyCode parameter should be a String of one of the ISO 4217 currency codes, for example 'USD'.

If there is an error obtaining the pattern, then the errorCallback executes with a [GlobalizationError](GlobalizationError/globalizationerror.html) object as a parameter. The error's expected code is [GlobalizationError](GlobalizationError/globalizationerror.html).FORMATTING\_ERROR.

Supported Platforms

  • Amazon Fire OS
  • Android
  • BlackBerry WebWorks 5.0+
  • iOS

Quick Example

When the browser is set to the en\_US locale and the selected currency is United States Dollars, this example displays a popup dialog with text similar to the results that follow:

navigator.globalization.getCurrencyPattern(
    'USD',
    function (pattern) {
        alert('pattern: '  + pattern.pattern  + '\n' +
              'code: '     + pattern.code     + '\n' +
              'fraction: ' + pattern.fraction + '\n' +
              'rounding: ' + pattern.rounding + '\n' +
              'decimal: '  + pattern.decimal  + '\n' +
              'grouping: ' + pattern.grouping);
    },
    function () { alert('Error getting pattern\n'); }
);

Expected result:

pattern: $#,##0.##;($#,##0.##)
code: USD
fraction: 2
rounding: 0
decimal: .
grouping: ,

Full Example

<!DOCTYPE HTML>
<html>
  <head>
    <title>getCurrencyPattern Example</title>
    <script type="text/javascript" charset="utf-8" src="cordova.js"></script>
    <script type="text/javascript" charset="utf-8">

    function checkPattern() {
      navigator.globalization.getCurrencyPattern(
        'USD',
        function (pattern) {alert('pattern: '  + pattern.pattern  + '\n' +
                                  'code: '     + pattern.code     + '\n' +
                                  'fraction: ' + pattern.fraction + '\n' +
                                  'rounding: ' + pattern.rounding + '\n' +
                                  'decimal: '  + pattern.decimal  + '\n' +
                                  'grouping: ' + pattern.grouping);},
        function () {alert('Error getting pattern\n');}
      );
    }

    </script>
  </head>
  <body>
    <button onclick="checkPattern()">Click for pattern</button>
  </body>
</html>