js/src/builtin/Number.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/builtin/Number.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,40 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +/*global intl_NumberFormat: false, */
     1.9 +
    1.10 +
    1.11 +var numberFormatCache = new Record();
    1.12 +
    1.13 +
    1.14 +/**
    1.15 + * Format this Number object into a string, using the locale and formatting options
    1.16 + * provided.
    1.17 + *
    1.18 + * Spec: ECMAScript Language Specification, 5.1 edition, 15.7.4.3.
    1.19 + * Spec: ECMAScript Internationalization API Specification, 13.2.1.
    1.20 + */
    1.21 +function Number_toLocaleString() {
    1.22 +    // Steps 1-2.  Note that valueOf enforces "this Number value" restrictions.
    1.23 +    var x = callFunction(std_Number_valueOf, this);
    1.24 +
    1.25 +    // Steps 2-3.
    1.26 +    var locales = arguments.length > 0 ? arguments[0] : undefined;
    1.27 +    var options = arguments.length > 1 ? arguments[1] : undefined;
    1.28 +
    1.29 +    // Step 4.
    1.30 +    var numberFormat;
    1.31 +    if (locales === undefined && options === undefined) {
    1.32 +        // This cache only optimizes for the old ES5 toLocaleString without
    1.33 +        // locales and options.
    1.34 +        if (numberFormatCache.numberFormat === undefined)
    1.35 +            numberFormatCache.numberFormat = intl_NumberFormat(locales, options);
    1.36 +        numberFormat = numberFormatCache.numberFormat;
    1.37 +    } else {
    1.38 +        numberFormat = intl_NumberFormat(locales, options);
    1.39 +    }
    1.40 +
    1.41 +    // Step 5.
    1.42 +    return intl_FormatNumber(numberFormat, x);
    1.43 +}

mercurial