1000Nettles
1000Nettles

Reputation: 2334

Why is PHP's NumberFormatter nickel-rounding formatted Swiss Franc amounts?

Using the formatCurrency() method on the NumberFormatter class with the CHF (Swiss Francs) currency is rounding amounts to the nearest nickel amount (0.05) for me.

Code:

$formatter = new \NumberFormatter('en_US', \NumberFormatter::CURRENCY);
echo $formatter->formatCurrency('12.34', 'CHF');

Result:

CHF12.35

Notice that the numeric amount 12.34 rounds up to 12.35. At first, I thought potentially this was expected and a new way of handling money that I am unfamiliar with. Turns out, that is not how it is supposed to work. (https://money.stackexchange.com/q/105014/81599)

Why is it rounding up / down to the nearest nickel amount in PHP, and how can I fix it?

Upvotes: 0

Views: 560

Answers (1)

1000Nettles
1000Nettles

Reputation: 2334

The answer actually lies outside of PHP, within the ICU library which PHP wraps via the Internationalization extension. As it turns out, I had an outdated version (50.x) of the ICU library which included the nickel-rounding bug within it. Updating to the nearest ICU library (as of today, 63.1) will allow you to use the most up-to-date internationalization standards, including this bugfix. I'm unsure which specific upgrade of the ICU fixed my issue, but I do know that the latest fixes this.

As for why this happened in the first place, Switzerland phased out the 0.01 denomination in their currency and the lowest denomination is actually 0.05 with cash amounts. Potentially, developers who work on the ICU got confused as reading, say, this Unicode chart may initially appear misleading.

Related:

Upvotes: 3

Related Questions