ZF-2878: bcmath extension used by Zend_Locale doesn't support scientific notation for floats representation.

Issue Type: Improvement Created: 2008-03-13T17:17:23.000+0000 Last Updated: 2009-01-03T05:31:36.000+0000 Status: Resolved Fix version(s): - 1.7.3 (19/Jan/09)

Reporter: Alexander Veremyev (alexander) Assignee: Thomas Weidner (thomas) Tags: - Zend_Locale

Related issues: - ZF-5374



bcmath extension uses strings as an input. Some floats are automatically written using scientific notation while they are converted to strings (like '1.234E-6').

bcmath extension skips exponent part of the number (e.g. '1.234E-6' => '1.234'). That produces wrong results of mathimatical operations with some numbers.

Hopefully, bcmath functions are not used directly within Zend Framework (except some big integers arithmetic within Zend_OpenId which is not affected by this problem). So we can proxy bcmath calls and perform correct numbers transformation.

That also may help to solve problems with some locales having ',' as decimal separator.


Posted by Maxence Delannoy (mdelanno) on 2008-12-30T00:17:07.000+0000

We can solve this by adding this code to Zend_Locale_Math::normalize:

<pre class="highlight">
public static function normalize($value)
  // Handle exponential notation
  $parts = preg_split('/e/i', $value);
  if (count($parts) > 1) {
    $value = bcmul($parts[0], bcpow(10, $parts[1]));
  return $value;

Test :

<pre class="highlight">
$this->assertEquals('1000', Zend_Locale_Math::normalize('1e3'));
$this->assertEquals('1000', Zend_Locale_Math::normalize('1E3'));
$this->assertEquals('1234', Zend_Locale_Math::normalize('1.234E3'));
$this->assertEquals('0.001', Zend_Locale_Math::normalize('1e-3'));

$this->assertEquals('12', Zend_Locale_Math::round('1.23e1'));
$this->assertEquals('12.3', Zend_Locale_Math::round('1.23e1', 1));

Note : bcmath is required.

Posted by Maxence Delannoy (mdelanno) on 2008-12-30T00:19:42.000+0000

{quote} Hopefully, bcmath functions are not used directly within Zend Framework {quote}

bcmath functions (and scientific notation) are used in Zend_Measure.

Posted by Thomas Weidner (thomas) on 2008-12-30T10:47:37.000+0000

There are several places where bcmath are used. But your code can not be used, as it does not work when bcmath is not available.

You can not use bcmath functions on a class which is used as workaround when no bcmath is installed.

Posted by Thomas Weidner (thomas) on 2009-01-03T05:31:34.000+0000

Feature added with r13500

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.