Globalize your Web Applications: PHP's Locale Formatting Classes [con't]
The NumberFormatter Class
The I18N classes are not the only option for displaying locale-specific numbers. The native NumberFormatter class accepts a locale name as its first constructor argument. Here are the constructor details:
NumberFormatter( string $locale , int $style [, string $pattern ] ): The
$localeparameter is the locale name (e.g. en_CA) in which the number would be formatted . The
$styleof the formatting must be one of the format style constants. If
PATTERN_RULEBASEDis passed then the number format is opened using the given pattern, which must conform to the syntax outlined by the
ICU RuleBasedNumberFormatconventions, respectively. The optional
$patternargument can be supplied in instances where the desired style requires a pattern.
Like the I18N classes, the NumberFormatter displays numbers via a versatile
string format ( number $value [, int $type ] ): Formats a numeric value according to the formatter rules.
Its parameters include:
- fmt: NumberFormatter object.
- value: The value to format. Can be integer or double, other values will be converted to a numeric value.
- type: The formatting type integer constant to use. These include:
- TYPE_DEFAULT: Derive the type from variable type.
- TYPE_INT32: Format as 32-bit integer.
- TYPE_INT64: Format as 64-bit integer.
- TYPE_DOUBLE: Format as floating point value.
- TYPE_CURRENCY: Format as currency value.
intl_is_failure()function with the results of
getErrorCode()to test for an error. The
intl_is_failure()function, which is one of several International functions, returns
trueif an error occurred and
falseotherwise. Although it is not shown in the example, it is also possible to retrieve the error message using the
The last type constant above of
TYPE_CURRENCY can be used in conjunction with
formatCurrency() function to display monetary values for a specific locale.
string formatCurrency ( float $value , string $currency ): Formats a currency value according to the formatter rules. Returns a string representing the formatted currency value.
Its parameters are:
- fmt: The NumberFormatter object.
- value: The numeric currency value.
- currency: The 3-letter ISO 4217 currency code indicating the currency to use.
Here's an example to illustrate:
If you're ever uncertain about what which code to use for a specific locale,
you can look it up on
the IBM site's excellent reference page..
That concludes our look at locale support in PHP. Next on our list is Java, the grand-daddy of Internet languages! It has a rich assortment of locale formatting classes and utility methods.
Rob Gravelle combined his love of programming and music to become a software guru and accomplished guitar player. He created systems that are used by Canada Border Services, CSIS and other Intelligence-related organizations. As a software consultant, Rob has developed Web applications for many businesses and recently created a MooTools version of PHPFreechat for ViziMetrics. Musically, Rob recently embarked on a solo music career, after playing with Ivory Knight since 2000. That band was rated as one Canada's top bands by Brave Words magazine (issue #92) and released two CDs. In 2007, Rob recorded the KNIGHTFALL CD in collaboration with the former Ivory Knight vocalist and legendary guitarist/producer, Jeff Waters of Annihilator fame. A completely FREE high quality MP3 download of his "Ultraviolence" intrumental, is availalable from his website, www.robgravelle.com. Rob is available for short-term software projects and recording session work. to inquire, but note that, due to the volume of emails received, he cannot respond to every email. Potential jobs and praise receive highest priority!
Original: December 28, 2009