| 1 |
// |reftest| skip-if(!this.hasOwnProperty("Intl")) |
| 2 |
|
| 3 |
// Nickel rounding. |
| 4 |
{ |
| 5 |
let nf = new Intl.NumberFormat("en", { |
| 6 |
minimumFractionDigits: 2, |
| 7 |
maximumFractionDigits: 2, |
| 8 |
roundingIncrement: 5, |
| 9 |
}); |
| 10 |
|
| 11 |
assertEq(nf.format(1.22), "1.20"); |
| 12 |
assertEq(nf.format(1.224), "1.20"); |
| 13 |
assertEq(nf.format(1.225), "1.25"); |
| 14 |
assertEq(nf.format(1.23), "1.25"); |
| 15 |
} |
| 16 |
|
| 17 |
// Dime rounding. |
| 18 |
{ |
| 19 |
let nf = new Intl.NumberFormat("en", { |
| 20 |
minimumFractionDigits: 2, |
| 21 |
maximumFractionDigits: 2, |
| 22 |
roundingIncrement: 10, |
| 23 |
}); |
| 24 |
|
| 25 |
assertEq(nf.format(1.24), "1.20"); |
| 26 |
assertEq(nf.format(1.249), "1.20"); |
| 27 |
assertEq(nf.format(1.250), "1.30"); |
| 28 |
assertEq(nf.format(1.25), "1.30"); |
| 29 |
} |
| 30 |
|
| 31 |
// Rounding increment option is rounded down. |
| 32 |
{ |
| 33 |
let nf1 = new Intl.NumberFormat("en", { |
| 34 |
minimumFractionDigits: 0, |
| 35 |
maximumFractionDigits: 0, |
| 36 |
roundingIncrement: 10, |
| 37 |
}); |
| 38 |
|
| 39 |
let nf2 = new Intl.NumberFormat("en", { |
| 40 |
minimumFractionDigits: 0, |
| 41 |
maximumFractionDigits: 0, |
| 42 |
roundingIncrement: 10.1, |
| 43 |
}); |
| 44 |
|
| 45 |
let nf3 = new Intl.NumberFormat("en", { |
| 46 |
minimumFractionDigits: 0, |
| 47 |
maximumFractionDigits: 0, |
| 48 |
roundingIncrement: 10.9, |
| 49 |
}); |
| 50 |
|
| 51 |
assertEq(nf1.resolvedOptions().roundingIncrement, 10); |
| 52 |
assertEq(nf2.resolvedOptions().roundingIncrement, 10); |
| 53 |
assertEq(nf3.resolvedOptions().roundingIncrement, 10); |
| 54 |
|
| 55 |
assertEq(nf1.format(123), "120"); |
| 56 |
assertEq(nf2.format(123), "120"); |
| 57 |
assertEq(nf3.format(123), "120"); |
| 58 |
} |
| 59 |
|
| 60 |
// |minimumFractionDigits| must be equal to |maximumFractionDigits| when |
| 61 |
// |roundingIncrement| is used. |
| 62 |
// |
| 63 |
// |minimumFractionDigits| defaults to zero. |
| 64 |
{ |
| 65 |
let nf = new Intl.NumberFormat("en", { |
| 66 |
roundingIncrement: 10, |
| 67 |
// minimumFractionDigits: 0, (default) |
| 68 |
maximumFractionDigits: 0, |
| 69 |
}); |
| 70 |
|
| 71 |
let resolved = nf.resolvedOptions(); |
| 72 |
assertEq(resolved.minimumFractionDigits, 0); |
| 73 |
assertEq(resolved.maximumFractionDigits, 0); |
| 74 |
assertEq(resolved.roundingIncrement, 10); |
| 75 |
|
| 76 |
assertEq(nf.format(123), "120"); |
| 77 |
assertEq(nf.format(123.456), "120"); |
| 78 |
} |
| 79 |
|
| 80 |
// |maximumFractionDigitsDefault| is set to |minimumFractionDigitsDefault| when |
| 81 |
// roundingIncrement isn't equal to 1. |
| 82 |
{ |
| 83 |
let options = { |
| 84 |
roundingIncrement: 10, |
| 85 |
// minimumFractionDigits: 0, (default) |
| 86 |
// maximumFractionDigits: 0, (default) |
| 87 |
}; |
| 88 |
let nf = new Intl.NumberFormat("en", options); |
| 89 |
assertEq(nf.resolvedOptions().minimumFractionDigits, 0); |
| 90 |
assertEq(nf.resolvedOptions().maximumFractionDigits, 0); |
| 91 |
} |
| 92 |
|
| 93 |
// |maximumFractionDigits| must be equal to |minimumFractionDigits| when |
| 94 |
// roundingIncrement isn't equal to 1. |
| 95 |
{ |
| 96 |
let options = { |
| 97 |
roundingIncrement: 10, |
| 98 |
// minimumFractionDigits: 0, (default) |
| 99 |
maximumFractionDigits: 1, |
| 100 |
}; |
| 101 |
assertThrowsInstanceOf(() => new Intl.NumberFormat("en", options), RangeError); |
| 102 |
} |
| 103 |
|
| 104 |
// Invalid values. |
| 105 |
for (let roundingIncrement of [-1, 0, Infinity, NaN]){ |
| 106 |
let options = { |
| 107 |
roundingIncrement, |
| 108 |
minimumFractionDigits: 0, |
| 109 |
maximumFractionDigits: 0, |
| 110 |
}; |
| 111 |
assertThrowsInstanceOf(() => new Intl.NumberFormat("en", options), RangeError); |
| 112 |
} |
| 113 |
|
| 114 |
if (typeof reportCompare === "function") |
| 115 |
reportCompare(true, true); |