summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/Intl/NumberFormat/rounding-priority.js
blob: 2e672a302231ebfaf3bbe6b4b02b57ff2621a9f2 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// |reftest| skip-if(!this.hasOwnProperty("Intl"))

const tests = [
  // Rounding conflict with maximum fraction/significand digits.
  {
    value: 4.321,
    options: {
      maximumFractionDigits: 2,
      maximumSignificantDigits: 2,
    },
    roundingPriorities: {
      auto: "4.3",
      lessPrecision: "4.3",
      morePrecision: "4.32",
    },
  },
  {
    value: 4.321,
    options: {
      maximumFractionDigits: 2,
      minimumFractionDigits: 2,
      maximumSignificantDigits: 2,
    },
    roundingPriorities: {
      auto: "4.3",
      lessPrecision: "4.3",
      morePrecision: "4.32",
    },
  },
  {
    value: 4.321,
    options: {
      maximumFractionDigits: 2,
      maximumSignificantDigits: 2,
      minimumSignificantDigits: 2,
    },
    roundingPriorities: {
      auto: "4.3",
      lessPrecision: "4.3",
      morePrecision: "4.32",
    },
  },
  {
    value: 4.321,
    options: {
      maximumFractionDigits: 2,
      minimumFractionDigits: 2,
      maximumSignificantDigits: 2,
      minimumSignificantDigits: 2,
    },
    roundingPriorities: {
      auto: "4.3",
      lessPrecision: "4.3",
      morePrecision: "4.32",
    },
  },

  // Rounding conflict with minimum fraction/significand digits.
  {
    value: 1.0,
    options: {
      minimumFractionDigits: 2,
      minimumSignificantDigits: 2,
    },
    roundingPriorities: {
      auto: "1.0",
      // Returning "1.00" for both options seems unexpected. Also filed at
      // <https://github.com/tc39/proposal-intl-numberformat-v3/issues/52>.
      lessPrecision: "1.00",
      morePrecision: "1.0",
    },
  },
  {
    value: 1.0,
    options: {
      minimumFractionDigits: 2,
      maximumFractionDigits: 2,
      minimumSignificantDigits: 2,
    },
    roundingPriorities: {
      auto: "1.0",
      lessPrecision: "1.00",
      morePrecision: "1.0",
    },
  },
  {
    value: 1.0,
    options: {
      minimumFractionDigits: 2,
      minimumSignificantDigits: 2,
      maximumSignificantDigits: 2,
    },
    roundingPriorities: {
      auto: "1.0",
      lessPrecision: "1.0",
      morePrecision: "1.00",
    },
  },
  {
    value: 1.0,
    options: {
      minimumFractionDigits: 2,
      maximumFractionDigits: 2,
      minimumSignificantDigits: 2,
      maximumSignificantDigits: 2,
    },
    roundingPriorities: {
      auto: "1.0",
      lessPrecision: "1.0",
      morePrecision: "1.00",
    },
  },
];

for (let {value, options, roundingPriorities} of tests) {
  for (let [roundingPriority, expected] of Object.entries(roundingPriorities)) {
    let nf = new Intl.NumberFormat("en", {...options, roundingPriority});
    assertEq(nf.resolvedOptions().roundingPriority, roundingPriority);
    assertEq(nf.format(value), expected, `value=${value}, roundingPriority=${roundingPriority}`);
  }
}

// Default value of "auto".
assertEq(new Intl.NumberFormat().resolvedOptions().roundingPriority, "auto");

// Invalid values.
for (let roundingPriority of ["", null, "more", "less", "never"]){
  assertThrowsInstanceOf(() => new Intl.NumberFormat("en", {roundingPriority}), RangeError);
}

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