Re: ICU - NumberFormatter::ROUNDING_MODE default ROUND_HALFEVEN

From: Date: Thu, 03 Aug 2023 20:02:12 +0000
Subject: Re: ICU - NumberFormatter::ROUNDING_MODE default ROUND_HALFEVEN
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-120884@lists.php.net to get a copy of this message
On Thursday 03 August 2023 21:07:35 (+02:00), Tim Düsterhus wrote: > Hi > > On 8/3/23 20:55, Hans Krentel via internals wrote: > > Feedback from actual ICU caretakers would be good though, because me > > is entirely from a users perspective, also I know how to change > > attributes to non default values, this is not a programming question > > I have. > > > Would the updated default also affect ->formatCurrency()? Yes, it exactly affects that, this was the case Q&A-ed: $formatter = new NumberFormatter('en', NumberFormatter::CURRENCY); $formatter->setAttribute( NumberFormatter::ROUNDING_MODE, NumberFormatter::ROUND_HALFUP ); echo $formatter->formatCurrency(31.005, 'USD'), "\n"; # Output: $31.01 > When formatting currencies, HALFEVEN is preferable to reduce the impact of rounding errors. Yes, for rounding, no for formatting. If I'm not mistaken: When rounding, HALFEVEN is preferred for the reason that it's done for multiple values being calculated further on, as it levitates rounding errors over the common number distributions in a line. When formatting, HALFUP is preferred for the reason that floating point numbers are with errors of the unit in the last place. Here it is demanded for as the formatting is for final display, not an ongoing calculation. As far as I could receive it over the years, users are happy with the results PHPs' number_format() yields to them. > > Best regards > Tim Düsterhus > Thank you for highlighting this feature of HALFEVEN Tim, it's also called _banker's rounding_ and is the default offering if I got this right from IEEE binary float for rounding. Which I assume could be one explanation for the current default. Best, -- hakre

« previous php.internals (#120884) next »