Skip to content

v3.34.0

Choose a tag to compare

@tjloong tjloong released this 03 Oct 12:08
· 24 commits to main since this release
ba24e79

⚠️ Behaviour change: currency() rounds half-up

num()->currency() now rounds half-up (away from zero) by default. It used to round half-even (banker's rounding, ICU's default). All 2dp output now rounds half-up. Only values with a 5 exactly at the 3rd decimal change:

value before now
10.185 10.18 10.19
0.125 0.12 0.13
-0.125 -0.12 -0.13

Every other value prints exactly as before: locale, grouping, negatives, bracket, symbol, 0.05 rounding and abbreviate are all unchanged. To keep the old rounding, pass roundingMode: 'half-even'.

New options

currency($in = null, $rounding = false, $bracket = false, $abbreviate = false, ?int $maxPrecision = null, string $roundingMode = 'half-up'): string
  • maxPrecision: N: at least 2, up to N decimals, trailing zeros trimmed. Use it for unit prices: num(0.065)->currency(maxPrecision: 6) → 0.065, 10 → 10.00.
  • roundingMode: 'half-up' (default), 'half-even' or 'half-down', any case. It does not affect the 0.05 $rounding step or abbreviate.
  • JS: Number.prototype.currency(symbol, round, maxPrecision) gains maxPrecision (clamped to 2–100).

Note: currency() formats the float it is given. A product computed in floats can land just under a tie, e.g. 175 * 9.825 = 1719.3749999999998 → 1,719.37. Pass stored or rounded totals.

For humblebear #401 (unit price cut to 2dp). PR #74.