Zend Framework

Zend_Currency::toCurrency() returning wrong numbers when working with small floats

Details

  • Type: Bug Bug
  • Status: Resolved Resolved
  • Priority: Major Major
  • Resolution: Fixed
  • Affects Version/s: 1.8.2
  • Fix Version/s: 1.9.0
  • Component/s: Zend_Currency
  • Labels:
    None

Description

Zend_Currency::toCurrency() returns wrong number when working with floats lower than 1.0E-5

Code example:

$zendCurrency = new Zend_Currency("USD", "en_US");
$value = 1.0E-4; // 0.0001
echo $zendCurrency->toCurrency($value) ; // outputs $0.00
$value = 1.0E-5; // 0.00001 from -5 it gets wrong
echo $zendCurrency->toCurrency($value) ; // outputs $1.0 - WRONG should be 0.00

Activity

Hide
Thomas Weidner added a comment -

The reason to this problem is based on PHP itself.

The scientific value "1.0E-4" is automatically converted to float "0.0001" by php.
But the scientific value "1.0E-5" is not converted.

As Zend_Currency can not handle scientific numbers for now you see the above mentioned result.
You can try this yourself by doing a var_dump on your inputs.

Show
Thomas Weidner added a comment - The reason to this problem is based on PHP itself. The scientific value "1.0E-4" is automatically converted to float "0.0001" by php. But the scientific value "1.0E-5" is not converted. As Zend_Currency can not handle scientific numbers for now you see the above mentioned result. You can try this yourself by doing a var_dump on your inputs.
Hide
Thomas Weidner added a comment -

Fixed with r16998

For details to this php bug see:
http://bugs.php.net/bug.php?id=43053

Show
Thomas Weidner added a comment - Fixed with r16998 For details to this php bug see: http://bugs.php.net/bug.php?id=43053
Hide
Dolf Schimmel (Freeaqingme) added a comment - - edited

Why don't you add a line like this to Zend_Currency::toCurrency(). All unittests seem to pass.

$value = ($value<1E-4 && $value > -1E-4)?0:$value;

Edit: Didn't see Thomas' second comment before posting mine.

Show
Dolf Schimmel (Freeaqingme) added a comment - - edited Why don't you add a line like this to Zend_Currency::toCurrency(). All unittests seem to pass.
$value = ($value<1E-4 && $value > -1E-4)?0:$value;
Edit: Didn't see Thomas' second comment before posting mine.

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: