Re: Re: [PHP] Round with money_format

2011-09-17 Thread Bill Guion
On Sep 17, 2011, at 3:46 AM, Cyril Lopez wrote:

 From: Cyril Lopez cy...@nethik.fr
 Date: September 16, 2011 10:58:28 AM EDT
 To: php-general@lists.php.net
 Subject: Round with money_format
 
 Hi,
 
 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 ?
 Thanks !
 
 Cyril
 
 Config :
 Debian Lenny, PHP 5.3.8

As someone else pointed out, rounding rules vary by locale, but I was taught 
40+ years ago in graduate school programming class, 4 rounds down, 6 rounds up, 
and 5 rounds to the even number. This means 65 rounds to 6, while 75 rounds to 
8. Your example seems to follow that rule.

 -= Bill =-
-- 

Virtuous action will never go unpunished.


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Round with money_format

2011-09-17 Thread Tedd Sperling
On Sep 17, 2011, at 10:56 AM, Bill Guion wrote:
 On Sep 17, 2011, at 3:46 AM, Cyril Lopez wrote:
 
 Can someone help me understand how money_format() rounds numbers ?
 
 As someone else pointed out, rounding rules vary by locale, but I was taught 
 40+ years ago in graduate school programming class, 4 rounds down, 6 rounds 
 up, and 5 rounds to the even number. This means 65 rounds to 6, while 75 
 rounds to 8. Your example seems to follow that rule.

Bill:

For many years (40+) I've claimed the same solution. I even wrote a routine to 
prove that algorithm is better than the one used in round().

The only problem is that it requires millions of instances to show any 
significant difference. IOW, it's simply not worth the effort.

Cheers,

tedd


_
t...@sperling.com
http://sperling.com


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: Re: [PHP] Round with money_format

2011-09-17 Thread Richard Quadling
On 17 September 2011 15:56, Bill Guion bgu...@comcast.net wrote:
 On Sep 17, 2011, at 3:46 AM, Cyril Lopez wrote:

 From: Cyril Lopez cy...@nethik.fr
 Date: September 16, 2011 10:58:28 AM EDT
 To: php-general@lists.php.net
 Subject: Round with money_format

 Hi,

 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 ?
 Thanks !

 Cyril

 Config :
 Debian Lenny, PHP 5.3.8

 As someone else pointed out, rounding rules vary by locale, but I was taught 
 40+ years ago in graduate school programming class, 4 rounds down, 6 rounds 
 up, and 5 rounds to the even number. This means 65 rounds to 6, while 75 
 rounds to 8. Your example seems to follow that rule.


I've been looking to see if I can find the locale's rounding rules - I can't.

Would be useful if anyone knows where they are defined.

As far as I can find (pages similar to
http://www.cl.cam.ac.uk/cgi-bin/manpage?7+locale), the locale data
doesn't seem to include the rounding rules. Sure, the number of DP,
grouping, etc. Yes. But no rounding.

So, does this just truncate?


-- 
Richard Quadling
Twitter : EE : Zend : PHPDoc
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY : bit.ly/lFnVea

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Round with money_format

2011-09-16 Thread Richard Quadling
On 16 September 2011 15:58, Cyril Lopez cy...@nethik.fr wrote:
 Hi,

 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 ?
 Thanks !

 Cyril

Each locale can define its own rules regarding the number of digits to
show, but I can't see anything that covers the rounding.

OOI.

What do you get for 12.66999?

I'm on windows and there's no money_format function (due to a lack of
strfmon() function),


-- 
Richard Quadling
Twitter : EE : Zend : PHPDoc
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY : bit.ly/lFnVea

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Round with money_format

2011-09-16 Thread Stuart Dallas
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

-- 
Stuart Dallas
3ft9 Ltd
http://3ft9.com/
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Round with money_format

2011-09-16 Thread Stuart Dallas
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/


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php