globalization.stringToNumber

Parses a number formatted as a string according to the client's user preferences and returns the corresponding number.

navigator.globalization.stringToNumber(string, successCallback, errorCallback, options);

Description

Returns the number to the successCallback with a properties object as a parameter. That object should have a value property with a Number value.

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

The options parameter is optional, and defaults to the following values:

{type:'decimal'}

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

Supported Platforms

  • Amazon Fire OS
  • Android
  • 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 number: 1234.56:

navigator.globalization.stringToNumber(
    '1234.56',
    function (number) {alert('number: ' + number.value + '\n');},
    function () {alert('Error getting number\n');},
    {type:'decimal'}
);

Full Example

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

    function checkNumber() {
      navigator.globalization.stringToNumber(
        '1234.56',
        function (number) {alert('number: ' + number.value + '\n');},
        function () {alert('Error getting number\n');},
        {type:'decimal'}
      );
    }

    </script>
  </head>
  <body>
    <button onclick="checkNumber()">Click for parsed number</button>
  </body>
</html>