|
|
When creating a measurement object, Zend_Measure_* methods expect the input/original measurement
data value as the first parameter. This can be a
numeric argument
, a
string
without units, or a
localized string with unit(s) specified.
The second parameter defines the type of the measurement. Both parameters are mandatory. The language may
optionally be specified as the third parameter.
In addition to integer data values, floating point types may be used, but » "simple decimal fractions like 0.1 or 0.7 cannot be converted into their internal binary counterparts without a little loss of precision," sometimes giving surprising results. Also, do not compare two "float" type numbers for equality.
Example #1 Creation using integer and floating values
Many measurements received as input to ZF applications can only be passed to Zend_Measure_*
classes as strings, such as numbers written using
» roman numerals
or extremely large binary values that exceed the precision of PHP's native integer and float types. Since
integers can be denoted using strings, if there is any risk of losing precision due to limitations of PHP's
native integer and float types, using strings instead. Zend_Measure_Number uses the BCMath
extension to support arbitrary precision, as shown in the example below, to avoid limitations in many PHP
functions, such as
» bin2dec()
.
Example #2 Creation using strings
Usually, Zend_Measure_* can automatically extract the desired measurement embedded in an
arbitrary string. Only the first identifiable number denoted using standard European/Latin digits
(0,1,2,3,4,5,6,7,8,9) will be used for measurement creation. If there are more numerals later in the string,
the rest of these numerals will be ignored.
Example #3 Arbitrary text input containing measurements
When a string is entered in a localized notation, the correct interpretation can not be determined without
knowing the intended locale. The division of decimal digits with "." and grouping of thousands with "," is
common in the English language, but not so in other languages. For example, the English number "1,234.50"
would be interpreted as meaning "1.2345" in German. To deal with such problems, the locale-aware
Zend_Measure_* family of classes offer the possibility to specify a language or region to
disambiguate the input data and properly interpret the intended semantic value.
Example #4 Localized string
|
|
Copyright © 2005-2011 Zend Technologies Inc (compiled by mikaelkael with ZFDocumentor - SVN 12849).

