Zend_Locale_Format
is a 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 respresentations, 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.
Default "root" Locale | |
---|---|
If no locale is specified, then normalization and localization will use the standard "root" locale, which might yield unexpected behavior, if the input originated in a different locale, or output for a specific locale was expected. |
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.
例 18.17. Number normalization
<?php require_once 'Zend/Locale.php'; $locale = new Zend_Locale('de_AT'); $number = Zend_Locale_Format::getNumber('13.524,678', array('locale' => $locale, 'precision' => 3)); print $number; // will return 13524.678 ?>
Since 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 truncated to a desired length with
getNumber()
. If no precision is given, no truncation occurs. Use only PHP integers to
specify the precision. The result will not be rounded. So "1.6" will return "1", not "2", if the
precision is zero.
toNumber($value, array $options = array())
can localize numbers to the
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()
.
例 18.19. Number localization
<?php require_once 'Zend/Locale.php'; $locale = new Zend_Locale('de_AT'); $number = Zend_Locale_Format::toNumber(13547.36, array('locale' => $locale)); // will return 13.547,36 print $number; ?>
Unlimited length | |
---|---|
|
The same way as within getNumber()
, toNumber()
handles precision. If no precision
is given, the complete localized number will be returned.
例 18.20. Number localization with precision
<?php require_once 'Zend/Locale.php'; $locale = new Zend_Locale('de_AT'); $number = Zend_Locale_Format::toNumber(13547.3678, array('precision' => 2, 'locale' => $locale)); // will return 13.547,36 print $number; ?>
Be aware | |
---|---|
|
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 seperation, precission and other number formatting signs from it. German for example defines ',' as precission seperation and in english the '.' sign is used.
表 18.2. Format tokens for self generated number formats
Token | Description | Example format | Generated output |
---|---|---|---|
#0 | Generates a number without precission and seperation | #0 | 1234567 |
, | Generates a seperation with the length from seperation to next seperation or to 0 | #,##0 | 1,234,567 |
#,##,##0 | Generates a standard seperation of 3 and all following seperations with 2 | #,##,##0 | 12,34,567 |
. | Generates a precission | #0.# | 1234567.1234 |
0 | Generates a precission with a defined length | #0.00 | 1234567.12 |
例 18.21. Using a self defined number format
<?php require_once 'Zend/Locale.php'; $locale = new Zend_Locale('de_AT'); $number = Zend_Locale_Format::toNumber(13547.3678, array('number_format' => '#,#0.00', 'locale' => 'de')); // will return 1.35.47,36 print $number; $number = Zend_Locale_Format::toNumber(13547.3, array('number_format' => '#,##0.00', 'locale' => 'de')); // will return 13.547,30 print $number; ?>
isNumber($value, array $options = array())
checks if a given string is a number and returns
true or false.
Floating point values can be parsed with the getFloat($value, array $options = array())
function. A floating point value will be returned.
toFloat()
can localize floating point values. This function will return a localized string of
the given number.
例 18.24. Floating point value localization
<?php require_once 'Zend/Locale.php'; $locale = new Zend_Locale('de_AT'); $number = Zend_Locale_Format::toFloat(13547.3655, array('precision' => 1, 'locale' => $locale)); // will return 13.547,3 print $number; ?>
Be aware | |
---|---|
|
isFloat($value, array $options = array())
checks if a given string is a floating point value
and returns true or false.
Integer values can be parsed with the getInteger()
function. A integer value will be returned.
toInteger($value, array $options = array())
can localize integer values. This function will
return a localized string of the given number.
isInteger($value, array $options = array())
checks if a given string is a integer value and
returns true or false.
Zend_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 it's 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
.
例 18.29. Converting numerals from Eastern Arabic scripts to European/Latin scripts
<?php require_once 'Zend/Locale.php'; $arabicScript = "١٠٠"; // Arabic for "100" (one hundred) $latinScript = Zend_Locale_Format::convertNumerals($arabicScript, 'Arab', 'Latn'); print "\nOriginal: " . $arabicScript; print "\nNormalized: " . $latinScript; ?>
Similarly, any of the supported numeral systems may be converted to any other supported numeral system.
例 18.30. Converting numerals from Latin script to Eastern Arabic script
<?php require_once 'Zend/Locale.php'; $latinScript = '123'; $arabicScript = Zend_Locale_Format::convertNumerals($latinScript, 'Latn', 'Arab'); print "\nOriginal: " . $latinScript; print "\nLocalized: " . $arabicScript; ?>
例 18.31. Getting 4 letter CLDR script code using a native-language name of the script
<?php function getScriptCode($scriptName, $locale) { $scripts2names = Zend_Locale_Data::getContent($locale, 'scriptlist'); $names2scripts = array_flip($scripts2names); return $names2scripts[$scriptName]; } echo getScriptCode('Latin', 'en'); // outputs "Latn" echo getScriptCode('Tamil', 'en'); // outputs "Taml" echo getScriptCode('tamoul', 'fr'); // outputs "Taml" ?>
表 18.3. List of supported numeral systems
Notation Name | Script |
---|---|
Arabic | Arab |
Balinese | Bali |
Bengali | Beng |
Devanagari | Deva |
Gujarati | Gujr |
Gurmukhi | Guru |
Kannada | Knda |
Khmer | Khmr |
Lao | Laoo |
Limbu | Limb |
Malayalam | Mlym |
Mongolian | Mong |
Myanmar | Mymr |
New_Tai_Lue | Talu |
Nko | Nkoo |
Oriya | Orya |
Tamil | Taml |
Telugu | Telu |
Thai | Tale |
Tibetan | Tibt |