ZF-5908: ::toNumber(-0.75, array('decimals' => 2)) == 0.75 -> should be -0.75 (with no bcmath extension)

Description

Formatting of float number between 0 and -1 loose the minus sign : example talk by itself :


echo Zend_Locale_Format::toNumber(-0.75, array('precision'=> 2, 'locale' => new Zend_Locale('en_EN'))); // -> Gives 0.75 -> ERROR MUST BE -0.75
echo Zend_Locale_Format::toNumber(-1.75, array('precision'=> 2, 'locale' => new Zend_Locale('en_EN'))); // -> Gives -1.75 -> OK
echo Zend_Locale_Format::toNumber(0.75, array('precision'=> 2, 'locale' => new Zend_Locale('en_EN'))); // -> Gives 0.75 -> OK

No bcmath extension enabled.

I can't provide a clean patch, but have a look in Zend_Locale_Format::toNumber() and especially to Zend_Locale_Math_PhpMath::Comp() method, you'll see what happens there.

Probably something linked to scale (in my case defaultScale is null) or better to review the comparisons when scale is null.

Good luck

PS :

A simple workaround could be something like below (in the end of method Zend_Locale_Format::toNumber())


        // set negative sign
        if (call_user_func(Zend_Locale_Math::$comp, $value, 0) < 0) {
            if (iconv_strpos($format, '-') === false) {
                $format = $symbols['minus'] . $format;
            } else {
                $format = str_replace('-', $symbols['minus'], $format);
            }
        }

        // patched in a hurry by Seb
        if ($value < 0 && $format >= 0) $format = $symbols['minus'] . "$format";
        
        return (string) $format;

Comments

Fixed with r14189

Thanks Thomas.

Quickest fix ever :D

Thanks again.