Zend Framework

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

Details

  • Type: Bug Bug
  • Status: Resolved Resolved
  • Priority: Major Major
  • Resolution: Fixed
  • Affects Version/s: 1.5.1, 1.5.2
  • Fix Version/s: 1.8.0
  • Component/s: Zend_Locale
  • Labels:
    None

Description

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.

Issue Links

Activity

Hide
Thomas Weidner added a comment -

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.

Show
Thomas Weidner added a comment - 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.
Hide
Brice Figureau added a comment -

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.
Show
Brice Figureau added a comment - 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.
Hide
Thomas Weidner added a comment -

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.

Show
Thomas Weidner added a comment - 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.
Hide
Thomas Weidner added a comment -

fixed with r13990

Show
Thomas Weidner added a comment - fixed with r13990
Hide
Michal Lasak added a comment -

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

Example from manual
$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.

Show
Michal Lasak added a comment - I've noticed when bcmath extension is enabled the problem reoccurs.
Example from manual
$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.
Hide
Thomas Weidner added a comment -

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.

Show
Thomas Weidner added a comment - 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.
Hide
Michal Lasak added a comment -

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

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

People

Vote (3)
Watch (4)

Dates

  • Created:
    Updated:
    Resolved: