Manipulating MeasurementsParsing and normalization of input, combined with output to localized notations makes data accessible to users in different locales. Many additional methods exist in Zend_Measure_* components to manipulate and work with this data, after it has been normalized. ConvertProbably the most important feature is the conversion into different units of measurement. The conversion of a unit can be done any number of times using the method convertTo(). Units of measurement can only be converted to other units of the same type (class). Therefore, it is not possible to convert (e.g.) a length into a weight, which would might encourage poor programming practices and allow errors to propagate without exceptions. The convertTo() method accepts an optional parameter. With this parameter you can define an precision for the returned output. The standard precision is '2'. Example #1 Convert
Add and subtractMeasurements can be added together using add() and subtracted using sub(). The result will use the same type as the originating object. Dynamic objects support a fluid style of programming, where complex sequences of operations can be nested without risk of side-effects altering the input objects.
Example #2 Adding units
Example #3 Subtract Subtraction of measurements works just like addition.
CompareMeasurements can also be compared, but without automatic unit conversion. Thus, equals() returns TRUE, only if both the value and the unit of measure are identical.
Example #4 Different measurements
Example #5 Identical measurements
CompareTo determine if a measurement is less than or greater than another, use compare(), which returns 0, -1 or 1 depending on the difference between the two objects. Identical measurements will return 0. Lesser ones will return a negative, greater ones a positive value.
Example #6 Difference
Manually change valuesTo change the value of a measurement explicitly, use setValue(). to overwrite the current value. The parameters are the same as the constructor.
Example #7 Changing a value
Manually change typesTo change the type of a measurement without altering its value use setType(). Example #8 Changing the type
|