Revision 386219 of Number.prototype.toLocaleString()

  • Revision slug: JavaScript/Reference/Global_Objects/Number/toLocaleString
  • Revision title: Number.prototype.toLocaleString
  • Revision id: 386219
  • Created:
  • Creator: Norbert
  • Is current revision? No
  • Comment

Revision Content

Summary

Returns a string with a language sensitive representation of this number. The new locales and options arguments let applications specify the language whose formatting conventions should be used and customize the behavior of the function. In older implementations, which ignore the locales and options arguments, the locale used and the form of the string returned are entirely implementation dependent.

Method of Number
Implemented in JavaScript 1.5
ECMAScript Edition ECMAScript Language Specification, 3rd Edition
ECMAScript Internationalization API Specification, 1st Edition

Syntax

number.toLocaleString([locales [, options]])

Parameters

Check the Browser compatibility section to see which browsers support the locales and options arguments, and the Example: Checking for support for locales and options arguments for feature detection.

{{page('en-US/docs/JavaScript/Reference/Global_Objects/NumberFormat','Parameters')}}

Examples

In basic use without specifying a locale, a formatted string in the default locale and with default options is returned.

var number = 3500;

console.log(number.toLocaleString()); // Displays "3,500" if in U.S. English locale

Example: Checking for support for locales and options arguments

The locales and options arguments are not supported in all browsers yet. To check whether an implementation supports them already, you can use the requirement that illegal language tags are rejected with a RangeError exception:

function toLocaleStringSupportsLocales() {
    var number = 0;
    try {
        number.toLocaleString("i");
    } catch (e) {
        return e​.name === "RangeError";
    }
    return false;
}

Example: Using locales

This example shows some of the variations in localized number formats. In order to get the format of the language used in the user interface of your application, make sure to specify that language (and possibly some fallback languages) using the locales argument:

var number = 123456.789;

// German uses comma as decimal separator and period for thousands
alert(number.toLocaleString("de-DE"));
// → 123.456,789

// Arabic in most Arabic speaking countries uses real Arabic digits
alert(number.toLocaleString("ar-EG"));
// → ١٢٣٤٥٦٫٧٨٩

// India uses thousands/lakh/crore separators
alert(number.toLocaleString("en-IN"));
// → 1,23,456.789

// the nu extension key requests a numbering system, e.g. Chinese decimal
alert(number.toLocaleString("zh-Hans-CN-u-nu-hanidec"));
// → 一二三,四五六.七八九

// when requesting a language that may not be supported, such as
// Balinese, include a fallback language, in this case Indonesian
alert(number.toLocaleString(["ban", "id"]));
// → 123.456,789

Example: Using options

The results provided by toLocaleString can be customized using the options argument:

var number = 123456.789;

// request a currency format
alert(number.toLocaleString("de-DE", {style: "currency", currency: "EUR"}));
// → 123.456,79 €

// the Japanese yen doesn't use a minor unit
alert(number.toLocaleString("ja-JP", {style: "currency", currency: "JPY"}))
// → ¥123,457

// limit to three significant digits
alert(number.toLocaleString("en-IN", {maximumSignificantDigits: 3}));
// → 1,23,000

Performance

When formatting large numbers of numbers, it is better to create an Intl.NumberFormat object and use the function provided by its format property.

Browser compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }}
locales and options arguments 24 {{CompatNightly("Firefox")}} {{ CompatNo() }} {{ CompatNo() }} {{ CompatNo() }}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }}
locales and options arguments {{ CompatNo() }} 26 {{ CompatNo() }} {{ CompatNo() }} {{ CompatNo() }} {{ CompatNo() }}

See also

Revision Source

<h2 id="Summary" name="Summary">Summary</h2>
<p>Returns a string with a language sensitive representation of this number. The new <code>locales</code> and <code>options</code> arguments let applications specify the language whose formatting conventions should be used and customize the behavior of the function. In older implementations, which ignore the <code>locales</code> and <code>options</code> arguments, the locale used and the form of the string returned are entirely implementation dependent.</p>
<table class="standard-table">
  <thead>
    <tr>
      <th class="header" colspan="2">Method of <a href="/en/JavaScript/Reference/Global_Objects/Number" title="en/JavaScript/Reference/Global_Objects/Number"><code>Number</code></a></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Implemented in</td>
      <td>JavaScript 1.5</td>
    </tr>
    <tr>
      <td rowspan="2">ECMAScript Edition</td>
      <td>ECMAScript Language Specification, 3<sup>rd</sup> Edition</td>
    </tr>
    <tr>
      <td>ECMAScript Internationalization API Specification, 1<sup>st</sup> Edition</td>
    </tr>
  </tbody>
</table>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
<code><em>number</em>.toLocaleString(</code><code>[locales [, options]])</code></pre>
<h3 id="Parameters">Parameters</h3>
<p>Check the <a href="#Browser_Compatibility" title="#Browser_Compatibility">Browser compatibility</a> section to see which browsers support the <code>locales</code> and <code>options</code> arguments, and the <a href="#Example:_Checking_for_support_for_locales_and_options_arguments">Example: Checking for support for <code>locales</code> and <code>options</code> arguments</a> for feature detection.</p>
<p>{{page('en-US/docs/JavaScript/Reference/Global_Objects/NumberFormat','Parameters')}}</p>
<h2 id="Examples" name="Examples">Examples</h2>
<p>In basic use without specifying a locale, a formatted string in the default locale and with default options is returned.</p>
<pre class="brush: js">
var number = 3500;

console.log(number.toLocaleString()); // Displays "3,500" if in U.S. English locale
</pre>
<h3 id="Example:_Checking_for_support_for_locales_and_options_arguments" name="Example:_Checking_for_support_for_locales_and_options_arguments">Example: Checking for support for <code>locales</code> and <code>options</code> arguments</h3>
<p>The <code>locales</code> and <code>options</code> arguments are not supported in all browsers yet. To check whether an implementation supports them already, you can use the requirement that illegal language tags are rejected with a <code>RangeError</code> exception:</p>
<pre class="brush: js">
function toLocaleStringSupportsLocales() {
    var number = 0;
    try {
        number.toLocaleString("i");
    } catch (e) {
        return e​.name === "RangeError";
    }
    return false;
}
</pre>
<h3 id="Example:_Using_locales" name="Example:_Using_locales">Example: Using <code>locales</code></h3>
<p>This example shows some of the variations in localized number formats. In order to get the format of the language used in the user interface of your application, make sure to specify that language (and possibly some fallback languages) using the <code>locales</code> argument:</p>
<pre class="brush: js">
var number = 123456.789;

// German uses comma as decimal separator and period for thousands
alert(number.toLocaleString("de-DE"));
// → 123.456,789

// Arabic in most Arabic speaking countries uses real Arabic digits
alert(number.toLocaleString("ar-EG"));
// → ١٢٣٤٥٦٫٧٨٩

// India uses thousands/lakh/crore separators
alert(number.toLocaleString("en-IN"));
// → 1,23,456.789

// the nu extension key requests a numbering system, e.g. Chinese decimal
alert(number.toLocaleString("zh-Hans-CN-u-nu-hanidec"));
// → 一二三,四五六.七八九

// when requesting a language that may not be supported, such as
// Balinese, include a fallback language, in this case Indonesian
alert(number.toLocaleString(["ban", "id"]));
// → 123.456,789
</pre>
<h3 id="Example:_Using_options" name="Example:_Using_options">Example: Using <code>options</code></h3>
<p>The results provided by <code>toLocaleString</code> can be customized using the <code>options</code> argument:</p>
<pre class="brush: js">
var number = 123456.789;

// request a currency format
alert(number.toLocaleString("de-DE", {style: "currency", currency: "EUR"}));
// → 123.456,79&nbsp;€

// the Japanese yen doesn't use a minor unit
alert(number.toLocaleString("ja-JP", {style: "currency", currency: "JPY"}))
// → ¥123,457

// limit to three significant digits
alert(number.toLocaleString("en-IN", {maximumSignificantDigits: 3}));
// → 1,23,000
</pre>
<h2 id="Performance" name="Performance">Performance</h2>
<p>When formatting large numbers of numbers, it is better to create an <a href="/en-US/docs/JavaScript/Reference/Global_Objects/NumberFormat" title="/en-US/docs/JavaScript/Reference/Global_Objects/NumberFormat"><code>Intl.NumberFormat</code></a> object and use the function provided by its <a href="/en-US/docs/JavaScript/Reference/Global_Objects/NumberFormat/format" title="/en-US/docs/JavaScript/Reference/Global_Objects/NumberFormat/format"><code>format</code></a> property.</p>
<h2 id="Browser_Compatibility" name="Browser_Compatibility">Browser compatibility</h2>
<p>{{ CompatibilityTable() }}</p>
<div id="compat-desktop">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Chrome</th>
        <th>Firefox (Gecko)</th>
        <th>Internet Explorer</th>
        <th>Opera</th>
        <th>Safari (WebKit)</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>{{ CompatVersionUnknown() }}</td>
        <td>{{ CompatVersionUnknown() }}</td>
        <td>{{ CompatVersionUnknown() }}</td>
        <td>{{ CompatVersionUnknown() }}</td>
        <td>{{ CompatVersionUnknown() }}</td>
      </tr>
      <tr>
        <td><code>locales</code> and <code>options</code> arguments</td>
        <td>24</td>
        <td>{{CompatNightly("Firefox")}}</td>
        <td>{{ CompatNo() }}</td>
        <td>{{ CompatNo() }}</td>
        <td>{{ CompatNo() }}</td>
      </tr>
    </tbody>
  </table>
</div>
<div id="compat-mobile">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Android</th>
        <th>Chrome for Android</th>
        <th>Firefox Mobile (Gecko)</th>
        <th>IE Phone</th>
        <th>Opera Mobile</th>
        <th>Safari Mobile</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>{{ CompatVersionUnknown() }}</td>
        <td>{{ CompatVersionUnknown() }}</td>
        <td>{{ CompatVersionUnknown() }}</td>
        <td>{{ CompatVersionUnknown() }}</td>
        <td>{{ CompatVersionUnknown() }}</td>
        <td>{{ CompatVersionUnknown() }}</td>
      </tr>
      <tr>
        <td><code>locales</code> and <code>options</code> arguments</td>
        <td>{{ CompatNo() }}</td>
        <td>26</td>
        <td>{{ CompatNo() }}</td>
        <td>{{ CompatNo() }}</td>
        <td>{{ CompatNo() }}</td>
        <td>{{ CompatNo() }}</td>
      </tr>
    </tbody>
  </table>
</div>
<h2 id="See_also">See also</h2>
<ul>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/NumberFormat" title="/en-US/docs/JavaScript/Reference/Global_Objects/NumberFormat">Intl.NumberFormat</a></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Number/toString" title="JavaScript/Reference/Global_Objects/Number/toString">Number.toString()</a></li>
</ul>
Revert to this revision