ZF-8009: Zend_Measure_Abstract forces precision in setType()

Description

I use Zend_Measure in one of my projects, and after upgrading from ZF 1.8.x to ZF 1.9.3, I began to get unexpected results.

In ZF 1.8.x, I could convert from measure A to measure B and receive a decimal number with the fractional part intact. I can no longer do that in 1.9.3. For example, in 1.8.x, the following returns 3.3100000000000000000000000 m:

$unit = new Zend_Measure_Length(231, Zend_Measure_Length::CENTIMETER, $locale); $unit2 = new Zend_Measure_Length(1, Zend_Measure_Length::METER, $locale);

$sum = $unit->add($unit2);

$sum->setType(Zend_Measure_Length::METER);

echo $sum;

The same code returns 3 m when using 1.9.3.

The culprit turns out to be a change in Zend_Measure_Abstract::setType() in REV 15906. In REV 15906 (included in ZF 1.9.x), setType() forces rounding to the precision of the provided value. Since the values in the code example above do not have decimal fractions, the returned value is rounded to a precision of 0 (Zend_Locale_Math::round($value, $prec) on line 292. Precision is set to 0 on line 240 and changes only if strpos($this->_value, '.') !== false (line 242). This makes the $round parameter in Zend_Measure_Abstract::getValue() (almost) worthless, since I don't have any control over the rounding precision in Zend_Measure_Abstract::setType() (unless I pass a measure with a decimal fraction). Very frustrating, very confusing, and unless I missed something, this is undocumented behavior.

Is this a bug or a feature? If it's a feature, could someone explain how this behavior is preferable to the behavior in ZF 1.8.x?

Comments

Fixed withr18493. Note that the returned value from 1.8 is also wrong.