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