globalization.getNumberPattern

Returns a pattern string to format and parse numbers according to the client's user preferences.

navigator.globalization.getNumberPattern(successCallback, errorCallback, options);

Description

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

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

  • symbol: The symbol to use when formatting and parsing, such as a percent or currency symbol. (String)

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

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

  • positive: The symbol to use for positive numbers when parsing and formatting. (String)

  • negative: The symbol to use for negative numbers when parsing and formatting. (String)

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

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

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).PATTERN\_ERROR.

The options parameter is optional, and default values are:

{type:'decimal'}

The options.type can be decimal, percent, or currency.

Supported Platforms

  • Amazon Fire OS
  • Android
  • BlackBerry WebWorks 5.0+
  • iOS
  • Windows Phone 8

Quick Example

When the browser is set to the en\_US locale, this should display a popup dialog with text similar to the results that follow:

navigator.globalization.getNumberPattern(
    function (pattern) {alert('pattern: '  + pattern.pattern  + '\n' +
                              'symbol: '   + pattern.symbol   + '\n' +
                              'fraction: ' + pattern.fraction + '\n' +
                              'rounding: ' + pattern.rounding + '\n' +
                              'positive: ' + pattern.positive + '\n' +
                              'negative: ' + pattern.negative + '\n' +
                              'decimal: '  + pattern.decimal  + '\n' +
                              'grouping: ' + pattern.grouping);},
    function () {alert('Error getting pattern\n');},
    {type:'decimal'}
);

Results:

pattern: #,##0.###
symbol: .
fraction: 0
rounding: 0
positive:
negative: -
decimal: .
grouping: ,

Full Example

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

    function checkPattern() {
      navigator.globalization.getNumberPattern(
        function (pattern) {alert('pattern: '  + pattern.pattern  + '\n' +
                                  'symbol: '   + pattern.symbol   + '\n' +
                                  'fraction: ' + pattern.fraction + '\n' +
                                  'rounding: ' + pattern.rounding + '\n' +
                                  'positive: ' + pattern.positive + '\n' +
                                  'negative: ' + pattern.negative + '\n' +
                                  'decimal: '  + pattern.decimal  + '\n' +
                                  'grouping: ' + pattern.grouping);},
        function () {alert('Error getting pattern\n');},
        {type:'decimal'}
      );
    }

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

Windows Phone 8 Quirks

  • The pattern property is not supported, and retuens an empty string.

  • The fraction property is not supported, and returns zero.