tolocalestring.js 2.12 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
// Copyright 2019 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

var locales = [
    "en",  // "1,234,567,890,123,456"
    "de",  // "1.234.567.890.123.456"
    "fr",  // "1 234 567 890 123 456"
    "hi",  // "1,23,45,67,89,01,23,456"
    "fa",  // "۱٬۲۳۴٬۵۶۷٬۸۹۰٬۱۲۳٬۴۵۶"
    "th-u-nu-thai",  // "๑,๒๓๔,๕๖๗,๘๙๐,๑๒๓,๔๕๖"
];

var data = [
    Number.MAX_SAFE_INTEGER,
    -Number.MAX_SAFE_INTEGER,
    Math.floor(Number.MAX_SAFE_INTEGER / 2),
    0,
    /// -0, // this case is broken now.
];

for (var locale of locales) {
  let nf = new Intl.NumberFormat(locale);

  let percentOption = {style: "percent"};
  let nfPercent = new Intl.NumberFormat(locale, percentOption);
  for (var n of data) {
    let bigint = BigInt(n);
    // Test NumberFormat w/ number output the same as
    // BigInt.prototype.toLocaleString()
    assertEquals(nf.format(n), bigint.toLocaleString(locale));

    // Test NumberFormat output the same regardless pass in as number or BigInt
    assertEquals(nf.format(n), nf.format(bigint));

    // Test formatToParts
    assertEquals(nf.formatToParts(n), nf.formatToParts(bigint));

    // Test output with option
    // Test NumberFormat w/ number output the same as
    // BigInt.prototype.toLocaleString()
    assertEquals(nfPercent.format(n),
        bigint.toLocaleString(locale, percentOption));

    // Test NumberFormat output the same regardless pass in as number or BigInt
    assertEquals(nfPercent.format(n), nfPercent.format(bigint));
    assertEquals(nfPercent.formatToParts(n), nfPercent.formatToParts(bigint));
  }

  // Test very big BigInt
  let veryBigInt = BigInt(Number.MAX_SAFE_INTEGER) *
      BigInt(Number.MAX_SAFE_INTEGER) *
      BigInt(Number.MAX_SAFE_INTEGER);
  assertEquals(nf.format(veryBigInt), veryBigInt.toLocaleString(locale));
  // It should output different than toString
  assertFalse(veryBigInt.toLocaleString(locale) == veryBigInt.toString());
  assertTrue(veryBigInt.toLocaleString(locale).length >
      veryBigInt.toString().length);
}