ZF-3473: Zend_Locale_Format::toNumber(9.72, array('locale' => 'en')) returns 9.7 instead of 9.72

Issue Type: Bug Created: 2008-06-18T03:27:13.000+0000 Last Updated: 2010-03-23T16:02:32.000+0000 Status: Resolved Fix version(s): - 1.8.0 (30/Apr/09)

Reporter: Brice Figureau (masterzen) Assignee: Thomas Weidner (thomas) Tags: - Zend_Locale

Related issues: - ZF-4729

Attachments: - fix-tonumber.patch


Zend_Locale_Format seems to not like float numbers starting with 9.

This is because of the following code in Zend/Locale/Format.php (around line 386 in 1.5.2): if (strlen($value) != strlen(Zend_Locale_Math::round($value, 0))) { if ($options['precision'] === null) { $precstr = iconv_substr($value, strlen(Zend_Locale_Math::round($value, 0)) + 1); } else {

Unfortunately round(9.72) == 10 which accounts for 2 characters instead of only one, hence the precstr length is only 1 character instead of the two it should have, and the final returned number is missing one charater.

A possible fix would be to not round, but use floor to get the decimal part size.


Posted by Thomas Weidner (thomas) on 2008-06-18T05:14:57.000+0000

This would not work as floor takes in account the locale, Zend_Locale_Math does not as it's locale independent.

So your solution would not work for other locales than english or the one near to english. Also your solution would only work with positive numbers.

So your attached patch does not solve the problem.

Posted by Brice Figureau (masterzen) on 2008-06-18T05:43:16.000+0000

I wasn't mentioning php floor() function, and indeed my patch doesn't use php floor. I just modified Zend_Locale_Math::round to not perform the +1 addition in case of a decimal digit > 5, in this case it does a kind of floor. And it seems to work for negative numbers.

IMHO the only caveats I could find are: * I didn't test non-latin locale (but I fail to see why it couldn't work) * I didn't implement the disabled bcmath part, hence the automated test would still fail.

Posted by Thomas Weidner (thomas) on 2008-06-18T13:05:15.000+0000

I just looked at the patch, havn't done this before as I just replied related to your comments. ;-)

We should not edit the round function as it's a locale independent version of PHP's round and has to work and behave like it.

So there are 2 ways I see: Eighter fix the string concentation within Format (which would be what I propose). Or add a locale independent floor and ceil functions which are then called.

Also to mention... all locale functions must work also with disabled bcmath, and actually do... so a test would show this and to implement the patch we would need the tests also.

If you would be willing to do this, I would implement it into SVN. Otherwise you would have to wait some time for me to have it fixed as I have much other work beside this one for ZF.

Thank you for your help anyway. I appreciate every help.

Posted by Thomas Weidner (thomas) on 2009-02-05T14:42:24.000+0000

fixed with r13990

Posted by Michal Lasak (mlasak) on 2010-03-23T15:06:49.000+0000

I've noticed when bcmath extension is enabled the problem reoccurs.

<pre class="highlight">
$number = Zend_Locale_Format::toNumber(13547.3678,
                                       array('precision' => 2,
                                             'locale' => $locale));

returns 13547.36 with bcmath enabled. Zend Server 5.0, ZF 1.10.2, PHP 5.2.12, Windows 7.

Posted by Thomas Weidner (thomas) on 2010-03-23T15:31:26.000+0000

Sorry, but your testcase is not what is described here as failure. It does not start with "9", but with "1".

Issue will keep resolved.

Posted by Michal Lasak (mlasak) on 2010-03-23T16:02:31.000+0000

Sorry, the correct issue should be ZF-4408. Can you move my previous comment there please?

Have you found an issue?

See the Overview section for more details.


© 2006-2018 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.