15.3. Normalization and Localization
Anterior | Capítulo 15. Zend_Locale | Próxima

## 15.3. Normalization and Localization

`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.

### 15.3.1. Number normalization: getNumber($input, $precision = false, $locale = false)

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.

Exemplo 15.15. Number normalization

    <?php
    require_once 'Zend/Locale.php';

    $locale = new Zend_Locale('de_AT');
    $number = Zend_Locale_Format::getNumber('13.524,678', false, $locale);

    print $number; // will return 13524.678
    ?>

#### 15.3.1.1. Precision and Calculations

Since `getNumber()` 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.

Exemplo 15.16. Number normalization with precision

    <?php
    require_once 'Zend/Locale.php';

    $locale = new Zend_Locale('de_AT');
    $number = Zend_Locale_Format::getNumber('13.524,678', 1, $locale);

    print $number; // will return 13524.6
    ?>

### 15.3.2. Number localization

`toNumber()` 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.

Exemplo 15.17. Number localization

    <?php
    require_once 'Zend/Locale.php';

    $locale = new Zend_Locale('de_AT');
    $number = Zend_Locale_Format::toNumber(13547.36, $locale);

    // will return 13.547,36
    print $number;
    ?>

| Unlimited length
`toNumber()` can localize numbers with unlimited length. It is not related to integer or float limitations.

The same way as within `getNumber()`, `toNumber()` handles precision. If no precision is given, the complete localized number will be returned.

Exemplo 15.18. Number localization with precision

    <?php
    require_once 'Zend/Locale.php';

    $locale = new Zend_Locale('de_AT');
    $number = Zend_Locale_Format::toNumber(13547.3678, 2, $locale);

    // will return 13.547,36
    print $number;
    ?>

| Be aware
`toNumber()` will truncate the output. The result will not be rounded. So 1.6 will not return 2 but 1 instead.

### 15.3.3. Number testing

`isNumber()` checks if a given string is a number and returns true or false.

Exemplo 15.19. Number testing

    <?php
    require_once 'Zend/Locale.php';

    $locale = new Zend_Locale();
    if (Zend_Locale_Format::isNumber('13.445,36', 'de_AT') {
        print "Number";
    } else {
        print "not a Number";
    }
    ?>

### 15.3.4. Float value normalization

Floating point values can be parsed with the `getFloat()` function. A floating point value will be returned.

Exemplo 15.20. Floating point value normalization

    <?php
    require_once 'Zend/Locale.php';

    $locale = new Zend_Locale('de_AT');
    $number = Zend_Locale_Format::getFloat('13.524,678', 2, $locale);

    // will return 13524.67
    print $number;
    ?>

### 15.3.5. Floating point value localization

`toFloat()` can localize floating point values. This function will return a localized string of the given number.

Exemplo 15.21. Floating point value localization

    <?php
    require_once 'Zend/Locale.php';

    $locale = new Zend_Locale('de_AT');
    $number = Zend_Locale_Format::toFloat(13547.3655, 1, $locale);

    // will return 13.547,3
    print $number;
    ?>

| Be aware
`toFloat()` will truncate the output. The result will not be rounded. So 1.6 will not return 2 but 1 instead.

### 15.3.6. Floating point value testing

`isFloat()` checks if a given string is a floating point value and returns true or false.

Exemplo 15.22. Floating point value testing

    <?php
    require_once 'Zend/Locale.php';

    $locale = new Zend_Locale('de_AT');
    if (Zend_Locale_Format::isFloat('13.445,36', $locale) {
        print "float";
    } else {
        print "not a float";
    }
    ?>

### 15.3.7. Integer value normalization

Integer values can be parsed with the `getInteger()` function. A integer value will be returned.

Exemplo 15.23. Integer value normalization

    <?php
    require_once 'Zend/Locale.php';

    $locale = new Zend_Locale('de_AT');
    $number = Zend_Locale_Format::getInteger('13.524,678', $locale);

    // will return 13524
    print $number;
    ?>

### 15.3.8. Integer point value localization

`toInteger()` can localize integer values. This function will return a localized string of the given number.

Exemplo 15.24. Integer value localization

    <?php
    require_once 'Zend/Locale.php';

    $locale = new Zend_Locale('de_AT');
    $number = Zend_Locale_Format::toInteger(13547.3655, $locale);

    // will return 13.547
    print $number;
    ?>

### 15.3.9. Integer value testing

`isInteger()` checks if a given string is a integer value and returns true or false.

Exemplo 15.25. Integer value testing

    <?php
    require_once 'Zend/Locale.php';

    $locale = new Zend_Locale('de_AT');
    if (Zend_Locale_Format::isInteger('13.445', $locale) {
        print "integer";
    } else {
        print "not a integer";
    }
    ?>

### 15.3.10. Numeral System Conversion

`Zend_Locale_Format::toNumberSystem()` converts digits between different numeral systems , including the standard Arabic/European 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 -i.e. no conversion of decimal format characters.

Suppose a web form collected a numeric input expressed using arabic digits "١‎٠٠". Most software and PHP functions expect input using Arabic numerals. Fortunately, converting this input to it's equivalent Arabic numerals "100" requires little effort using `toNumberSystem($input, $sourceNumeralSystem, $destNumeralSystem)`, which returns the `$input` with numerals in `$sourceNumeralSystem` converted to `$destNumeralSystem`.

Exemplo 15.26. Converting numerals to Arabic/European

    <?php
    require_once 'Zend/Locale.php';

    $string = "١‎٠٠";   // Arabic for 100
    $normal = Zend_Locale_Format::toNumberSystem($string, 'Arab', 'Euro');

    print "\nOriginal: ".$string;
    print "\nNormalized:".$normal;
    ?>

Similarly, any of the supported numeral systems may be converted to any other supported numeral system.

Exemplo 15.27. Converting numerals from Arabic/European

    <?php
    require_once 'Zend/Locale.php';

    $string = '123';
    $local = Zend_Locale_Format::toNumberSystem($string, 'Euro', 'Arab');

    print "\nOriginal: ".$string;
    print "\nLocalize:".$local;
    ?>

#### 15.3.10.1. List of supported numeral systems

Tabela 15.1. 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

Anterior | Acima | Próxima
15.2. Using Zend_Locale | Principal | 15.4. Working with Dates and Times