Re: Round with money_format
| From: | Stuart Dallas | Date: | Fri, 16 Sep 2011 15:37:49 +0000 |
| Subject: | Re: Round with money_format | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-314907@lists.php.net to get a copy of this message | ||
Oops...
On 16 Sep 2011, at 16:36, Stuart Dallas wrote:
> On 16 Sep 2011, at 15:58, Cyril Lopez wrote:
>
>> Can someone help me understand how money_format() rounds numbers ?
>>
>> <?php
>> setlocale(LC_ALL, 'fr_FR.UTF-8');
>> $price = 12.665;
>> echo money_format('%i',$price);
>> // 12.66 EUR, 12.67 EUR expected
>>
>> $price2 = 12.666;
>> echo money_format('%i',$price2);
>> // 12.67 EUR, ok
>>
>> echo round($price,2);
>> // 12.67, ok
>> echo round($price2,2);
>> // 12.67, ok
>> ?>
>>
>> Misconfiguration ? Bug ?
>
> It's a direct mapping to the libc function, strfmon [1], and an equivalent C script gave
> the same result so it's not a bug in PHP. Locales can't define rounding rules, so
> it's not related to that.
>
> Googling for strfmon rounding errors doesn't get any useful results, so either this is by
> design or it's never caused anyone a problem.
>
> Solution... pass the number through round before going in to money_format.
>
> -Stuart
[1] http://lxr.php.net/opengrok/xref/PHP_5_3/ext/standard/string.c#5156
> --
> Stuart Dallas
> 3ft9 Ltd
> http://3ft9.com/