PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Number localization

toNumber($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 567. Number localization

<?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

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.

Example 568. Number localization with precision

<?php
$locale 
= new Zend_Locale('de_AT');
$number Zend_Locale_Format::toNumber(13547.3678,
                                       array(
'precision' => 2,
                                             
'locale' => $locale));

// will return 13.547,37
print $number;

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.

Table 107. Format tokens for self generated number formats

Token Description Example format Generated output
#0 Generates a number without precision and separation #0 1234567
, Generates a separation with the length from separation to next separation or to 0 #,##0 1,234,567
#,##,##0 Generates a standard separation of 3 and all following separations with 2 #,##,##0 12,34,567
. Generates a precision #0.# 1234567.1234
0 Generates a precision with a defined length #0.00 1234567.12

Example 569. Using a self defined number format

<?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;

Zend Framework