There is a known rounding failure by PHP.
Having this failure the automatic conversion works only when you define a precision and have a precision value.

There are 3 possible work arounds.

1.) Add a precision to your value and object
2.) Use trunk release (a workaround has been added some weeks ago)
3.) Wait for the next minor release

Greetings
Thomas Weidner, I18N Team Leader, Zend Framework
http://www.thomasweidner.com

----- Original Message ----- From: "J DeBord" <[email protected]>
To: "fw-general" <[email protected]>
Sent: Wednesday, October 14, 2009 2:55 PM
Subject: [fw-general] Zend_Measure undesired rounding when converting values


Zend_Measure_Length::convertTo rounds the values. I would like 2520 meters
to be converted to 2.52 km, not 3 km.

I'm storing distances as meters in a mysql database. I'm using Zend_Db to
pull the values.

I create a new Zend_Measure_Length object with the distance value from mysql
typecast to an integer. For example:

//$meters = (int) $result['distance']; // (if pulling from db)

$meters = 2520; // For illustrative purposes

$distance = new Zend_Measure_Length($meters, Zend_Measure_Length::STANDARD);

echo $distance . '<br>'; // outputs 2,520 m

$distance->convertTo(Zend_Measure_Length::KILOMETER);

echo $distance . '<br>'; // outputs 3 km

echo $distance->toString(2); . '<br>'; // outputs 3.0 km

A var_dump of $distance immediately after calling convertTo shows the value
property with a value of 3.

I would like and had expected the output to be 2.52 km.

The same thing happens when converting seconds to minutes.

Where am I going wrong? Am I overlooking a configuration option somewhere?

Thanks,

Jason


Reply via email to