format.js (2332B)
1 // |reftest| skip-if(!this.hasOwnProperty("Intl")) 2 /* This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 // Tests the format function with a diverse set of locales and options. 7 8 var format; 9 10 // Locale en-US; default options. 11 format = new Intl.NumberFormat("en-us"); 12 assertEq(format.format(0), "0"); 13 assertEq(format.format(-1), "-1"); 14 assertEq(format.format(123456789.123456789), "123,456,789.123"); 15 16 // Locale en-US; currency USD. 17 // The US dollar uses two fractional digits, and negative values are commonly 18 // parenthesized. 19 format = new Intl.NumberFormat("en-us", {style: "currency", currency: "USD"}); 20 assertEq(format.format(0), "$0.00"); 21 assertEq(format.format(-1), "-$1.00"); 22 assertEq(format.format(123456789.123456789), "$123,456,789.12"); 23 24 // Locale ja-JP; currency JPY. 25 // The Japanese yen has no subunit in real life. 26 format = new Intl.NumberFormat("ja-jp", {style: "currency", currency: "JPY"}); 27 assertEq(format.format(0), "¥0"); 28 assertEq(format.format(-1), "-¥1"); 29 assertEq(format.format(123456789.123456789), "¥123,456,789"); 30 31 // Locale ar-JO; currency JOD. 32 // The Jordanian Dinar divides into 1000 fils. Jordan uses (real) Arabic digits. 33 format = new Intl.NumberFormat("ar-jo", {style: "currency", currency: "JOD"}); 34 assertEq(format.format(0), "\u{200F}٠٫٠٠٠ د.أ.\u{200F}"); 35 assertEq(format.format(-1), "\u{061C}-\u{200F}١٫٠٠٠ د.أ.\u{200F}"); 36 assertEq(format.format(123456789.123456789), "\u{200F}١٢٣٬٤٥٦٬٧٨٩٫١٢٣ د.أ.\u{200F}"); 37 38 // Locale th-TH; Thai digits, percent, two significant digits. 39 format = new Intl.NumberFormat("th-th-u-nu-thai", 40 {style: "percent", 41 minimumSignificantDigits: 2, 42 maximumSignificantDigits: 2}); 43 assertEq(format.format(0), "๐.๐%"); 44 assertEq(format.format(-0.01), "-๑.๐%"); 45 assertEq(format.format(1.10), "๑๑๐%"); 46 47 48 // Test the .name property of the "format" getter. 49 var desc = Object.getOwnPropertyDescriptor(Intl.NumberFormat.prototype, "format"); 50 assertEq(desc !== undefined, true); 51 assertEq(typeof desc.get, "function"); 52 assertEq(desc.get.name, "get format"); 53 54 55 reportCompare(0, 0, 'ok');