summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/Intl/NumberFormat/rounding-increment.js
blob: 84d448022759de5b3b73435e5acf8ca19f499e73 (plain)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// |reftest| skip-if(!this.hasOwnProperty("Intl")||release_or_beta)

// Nickel rounding.
{
  let nf = new Intl.NumberFormat("en", {
    minimumFractionDigits: 2,
    maximumFractionDigits: 2,
    roundingIncrement: 5,
  });

  assertEq(nf.format(1.22), "1.20");
  assertEq(nf.format(1.224), "1.20");
  assertEq(nf.format(1.225), "1.25");
  assertEq(nf.format(1.23), "1.25");
}

// Dime rounding.
{
  let nf = new Intl.NumberFormat("en", {
    minimumFractionDigits: 2,
    maximumFractionDigits: 2,
    roundingIncrement: 10,
  });

  assertEq(nf.format(1.24), "1.20");
  assertEq(nf.format(1.249), "1.20");
  assertEq(nf.format(1.250), "1.30");
  assertEq(nf.format(1.25), "1.30");
}

// Rounding increment option is rounded down.
{
  let nf1 = new Intl.NumberFormat("en", {
    minimumFractionDigits: 0,
    maximumFractionDigits: 0,
    roundingIncrement: 10,
  });

  let nf2 = new Intl.NumberFormat("en", {
    minimumFractionDigits: 0,
    maximumFractionDigits: 0,
    roundingIncrement: 10.1,
  });

  let nf3 = new Intl.NumberFormat("en", {
    minimumFractionDigits: 0,
    maximumFractionDigits: 0,
    roundingIncrement: 10.9,
  });

  assertEq(nf1.resolvedOptions().roundingIncrement, 10);
  assertEq(nf2.resolvedOptions().roundingIncrement, 10);
  assertEq(nf3.resolvedOptions().roundingIncrement, 10);

  assertEq(nf1.format(123), "120");
  assertEq(nf2.format(123), "120");
  assertEq(nf3.format(123), "120");
}

// |minimumFractionDigits| must be equal to |maximumFractionDigits| when
// |roundingIncrement| is used.
//
// |minimumFractionDigits| defaults to zero.
{
  let nf = new Intl.NumberFormat("en", {
    roundingIncrement: 10,
    // minimumFractionDigits: 0, (default)
    maximumFractionDigits: 0,
  });

  let resolved = nf.resolvedOptions();
  assertEq(resolved.minimumFractionDigits, 0);
  assertEq(resolved.maximumFractionDigits, 0);
  assertEq(resolved.roundingIncrement, 10);

  assertEq(nf.format(123), "120");
  assertEq(nf.format(123.456), "120");
}

// |maximumFractionDigits| defaults to three. And because |0 !== 3|, a
// RangeError is thrown.
{
  let options = {
    roundingIncrement: 10,
    // minimumFractionDigits: 0, (default)
    // maximumFractionDigits: 3, (default)
  };
  assertThrowsInstanceOf(() => new Intl.NumberFormat("en", options), RangeError);
}

// Invalid values.
for (let roundingIncrement of [-1, 0, Infinity, NaN]){
  let options = {
    roundingIncrement,
    minimumFractionDigits: 0,
    maximumFractionDigits: 0,
  };
  assertThrowsInstanceOf(() => new Intl.NumberFormat("en", options), RangeError);
}

if (typeof reportCompare === "function")
  reportCompare(true, true);