Normalization and LocalizationZend_Locale_Format is an internal component used by Zend_Locale. All locale aware classes use Zend_Locale_Format for normalization and localization of numbers and dates. Normalization involves parsing input from a variety of data representations, like dates, into a standardized, structured representation, such as a PHP array with year, month, and day elements. The exact same string containing a number or a date might mean different things to people with different customs and conventions. Disambiguation of numbers and dates requires rules about how to interpret these strings and normalize the values into a standardized data structure. Thus, all methods in Zend_Locale_Format require a locale in order to parse the input data.
Number normalization: getNumber($input, Array $options)There are many » number systems different from the common » decimal system (e.g. "3.14"). Numbers can be normalized with the getNumber() function to obtain the standard decimal representation. for all number-related discussions in this manual, » Arabic/European numerals (0,1,2,3,4,5,6,7,8,9) are implied, unless explicitly stated otherwise. The options array may contain a 'locale' to define grouping and decimal characters. The array may also have a 'precision' to truncate excess digits from the result. Example #1 Number normalization
Precision and CalculationsSince getNumber($value, array $options = array()) can normalize extremely large numbers, check the result carefully before using finite precision calculations, such as ordinary PHP math operations. For example, if ((string)int_val($number) != $number) { use » BCMath or » GMP. Most PHP installations support the BCMath extension. Also, the precision of the resulting decimal representation can be rounded to a desired length with getNumber() with the option 'precision'. If no precision is given, no rounding occurs. Use only PHP integers to specify the precision. If the resulting decimal representation should be truncated to a desired length instead of rounded the option 'number_format' can be used instead. Define the length of the decimal representation with the desired length of zeros. The result will then not be rounded. So if the defined precision within number_format is zero the value "1.6" will return "1", not "2. See the example nearby: Example #2 Number normalization with precision
Number localizationtoNumber($value, array $options = array()) can localize numbers to the following supported locales. This function will return a localized string of the given number in a conventional format for a specific locale. The 'number_format' option explicitly specifies a non-default number format for use with toNumber(). Example #3 Number localization
The same way as within getNumber(), toNumber() handles precision. If no precision is given, the complete localized number will be returned. Example #4 Number localization with precision
Using the option 'number_format' a self defined format for generating a number can be defined. The format itself has to be given in CLDR format as described below. The locale is used to get separation, precision and other number formatting signs from it. German for example defines ',' as precision separation and in English the '.' sign is used.
Example #5 Using a self defined number format
Number testingisNumber($value, array $options = array()) checks if a given string is a number and returns TRUE or FALSE. Example #6 Number testing
Float value normalizationFloating point values can be parsed with the getFloat($value, array $options = array()) function. A floating point value will be returned. Example #7 Floating point value normalization
Floating point value localizationtoFloat() can localize floating point values. This function will return a localized string of the given number. Example #8 Floating point value localization
Floating point value testingisFloat($value, array $options = array()) checks if a given string is a floating point value and returns TRUE or FALSE. Example #9 Floating point value testing
Integer value normalizationInteger values can be parsed with the getInteger() function. A integer value will be returned. Example #10 Integer value normalization
Integer point value localizationtoInteger($value, array $options = array()) can localize integer values. This function will return a localized string of the given number. Example #11 Integer value localization
Integer value testingisInteger($value, array $options = array()) checks if a given string is an integer value and returns TRUE or FALSE. Example #12 Integer value testing
Numeral System ConversionZend_Locale_Format::convertNumerals() converts digits between different » numeral systems, including the standard Arabic/European/Latin numeral system (0,1,2,3,4,5,6,7,8,9), not to be confused with » Eastern Arabic numerals sometimes used with the Arabic language to express numerals. Attempts to use an unsupported numeral system will result in an exception, to avoid accidentally performing an incorrect conversion due to a spelling error. All characters in the input, which are not numerals for the selected numeral system, are copied to the output with no conversion provided for unit separator characters. Zend_Locale* components rely on the data provided by CLDR (see their » list of scripts grouped by language). In CLDR and hereafter, the Europena/Latin numerals will be referred to as "Latin" or by the assigned 4-letter code "Latn". Also, the CLDR refers to this numeral systems as "scripts". Suppose a web form collected a numeric input expressed using Eastern Arabic digits "١٠٠". Most software and PHP functions expect input using Arabic numerals. Fortunately, converting this input to its equivalent Latin numerals "100" requires little effort using convertNumerals($inputNumeralString, $sourceNumeralSystem, $destNumeralSystem), which returns the $input with numerals in the script $sourceNumeralSystem converted to the script $destNumeralSystem. Example #13 Converting numerals from Eastern Arabic scripts to European/Latin scripts
Similarly, any of the supported numeral systems may be converted to any other supported numeral system. Example #14 Converting numerals from Latin script to Eastern Arabic script
Example #15 Getting 4 letter CLDR script code using a native-language name of the script
For a list of supported numeral systems call Zend_Locale::getTranslationList('numberingsystem', 'en').
|