|
1 | 1 | // Example with different rounding modes
|
| 2 | + |
| 3 | +// Examples from the Intl.NumberFormat spec |
| 4 | + |
| 5 | +let minusOnePointFive = new Decimal128("-1.5"); |
| 6 | +let zeroPointFour = new Decimal128("0.4"); |
| 7 | +let zeroPointFive = new Decimal128("0.5"); |
| 8 | +let zeroPointSix = new Decimal128("0.6"); |
| 9 | +let onePointFive = new Decimal128("1.5"); |
| 10 | + |
| 11 | +// ceiling |
| 12 | +"-1" === minusOnePointFive.round(0, "ceil").toString(); |
| 13 | +"1" === zeroPointFour.round(0, "ceil").toString(); |
| 14 | +"1" === zeroPointFive.round(0, "ceil").toString(); |
| 15 | +"1" === zeroPointSix.round(0, "ceil").toString(); |
| 16 | +"2" === onePointFive.round(0, "ceil").toString(); |
| 17 | + |
| 18 | +// floor |
| 19 | +"-2" === minusOnePointFive.round(0, "floor").toString(); |
| 20 | +"0" === zeroPointFour.round(0, "floor").toString(); |
| 21 | +"0" === zeroPointFive.round(0, "floor").toString(); |
| 22 | +"0" === zeroPointSix.round(0, "floor").toString(); |
| 23 | +"1" === onePointFive.round(0, "floor").toString(); |
| 24 | + |
| 25 | +// expand |
| 26 | +"-2" === minusOnePointFive.round(0, "expand").toString(); |
| 27 | +"1" === zeroPointFour.round(0, "expand").toString(); |
| 28 | +"1" === zeroPointFive.round(0, "expand").toString(); |
| 29 | +"1" === zeroPointSix.round(0, "expand").toString(); |
| 30 | +"2" === onePointFive.round(0, "expand").toString(); |
| 31 | + |
| 32 | +// truncate |
| 33 | +"-1" === minusOnePointFive.round(0, "trunc").toString(); |
| 34 | +"0" === zeroPointFour.round(0, "trunc").toString(); |
| 35 | +"0" === zeroPointFive.round(0, "trunc").toString(); |
| 36 | +"0" === zeroPointSix.round(0, "trunc").toString(); |
| 37 | +"1" === onePointFive.round(0, "trunc").toString(); |
| 38 | + |
| 39 | +// round ties to ceiling |
| 40 | +"-1" === minusOnePointFive.round(0, "halfCeil").toString(); |
| 41 | +"0" === zeroPointFour.round(0, "halfCeil").toString(); |
| 42 | +"1" === zeroPointFive.round(0, "halfCeil").toString(); |
| 43 | +"1" === zeroPointSix.round(0, "halfCeil").toString(); |
| 44 | +"2" === onePointFive.round(0, "halfCeil").toString(); |
| 45 | + |
| 46 | +// round ties to floor |
| 47 | +"-2" === minusOnePointFive.round(0, "halfFloor").toString(); |
| 48 | +"0" === zeroPointFour.round(0, "halfFloor").toString(); |
| 49 | +"0" === zeroPointFive.round(0, "halfFloor").toString(); |
| 50 | +"1" === zeroPointSix.round(0, "halfFloor").toString(); |
| 51 | +"1" === onePointFive.round(0, "halfFloor").toString(); |
| 52 | + |
| 53 | +// round ties away from zero |
| 54 | +"-2" === minusOnePointFive.round(0, "halfExpand").toString(); |
| 55 | +"0" === zeroPointFour.round(0, "halfExpand").toString(); |
| 56 | +"1" === zeroPointFive.round(0, "halfExpand").toString(); |
| 57 | +"1" === zeroPointSix.round(0, "halfExpand").toString(); |
| 58 | +"2" === onePointFive.round(0, "halfExpand").toString(); |
| 59 | + |
| 60 | +// round ties to toward zero |
| 61 | +"-1" === minusOnePointFive.round(0, "halfTrunc").toString(); |
| 62 | +"0" === zeroPointFour.round(0, "halfTrunc").toString(); |
| 63 | +"0" === zeroPointFive.round(0, "halfTrunc").toString(); |
| 64 | +"1" === zeroPointSix.round(0, "halfTrunc").toString(); |
| 65 | +"1" === onePointFive.round(0, "halfTrunc").toString(); |
| 66 | + |
| 67 | +// round ties to even |
| 68 | +"-2" === minusOnePointFive.round(0, "halfEven").toString(); |
| 69 | +"0" === zeroPointFour.round(0, "halfEven").toString(); |
| 70 | +"0" === zeroPointFive.round(0, "halfEven").toString(); |
| 71 | +"1" === zeroPointSix.round(0, "halfEven").toString(); |
| 72 | +"2" === onePointFive.round(0, "halfEven").toString(); |
0 commit comments