System.I18N.core
[ class tree: System.I18N.core ] [ index: System.I18N.core ] [ all elements ]

Class: NumberFormat

Source Location: /I18N/core/NumberFormat.php

Class Overview


NumberFormat class.

Class Details

[line 74]
NumberFormat class.

NumberFormat formats decimal numbers in any locale. The decimal number is formatted according to a particular pattern. These patterns can arise from the NumberFormatInfo object which is culturally sensitive. The NumberFormat class can be instantiated in many ways. E.g.

  1. //create a invariant number formatter.
  2. $formatter = new NumberFormat();
  3.  
  4. //create a number format for the french language locale.
  5. $fr = new NumberFormat('fr');
  6.  
  7. //create a number format base on a NumberFormatInfo instance $numberInfo.
  8. $format = new NumberFormat($numberInfo);

A normal decimal number can also be displayed as a currency or as a percentage. For example

  1. $format->format(1234.5); //Decimal number "1234.5"
  2. $format->format(1234.5,'c'); //Default currency "$1234.50"
  3. $format->format(0.25, 'p')

Currency is formated using the localized currency pattern. For example to format the number as Japanese Yen:

  1. $ja = new NumberFormat('ja_JP');
  2.  
  3. //Japanese currency pattern, and using Japanese Yen symbol
  4. $ja->format(123.14,'c','JPY');
For each culture, the symbol for each currency may be different.




version:  v1.0, last update on Fri Dec 10 18:10:20 EST 2004
author:  Xiang Wei Zhuo <weizhuo[at]gmail[dot]com>


[ Top ]

Variables

Methods



Class Variables

$formatInfo =

[line 81]

The DateTimeFormatInfo, containing culture specific patterns and names.



access:  protected

Type:   DateTimeFormatInfo


[ Top ]



Class Methods


constructor __construct [line 91]

NumberFormat __construct( [mixed $formatInfo = null])

Create a new number format instance. The constructor can be instantiated with a string that represent a culture/locale. Similarly, passing a CultureInfo or NumberFormatInfo instance will instantiated a instance for that particular culture.



Parameters:

mixed   $formatInfo   either null, a CultureInfo, a NumberFormatInfo, or string

[ Top ]

method format [line 116]

string format( mixed $number, [string $pattern = 'd'], [string $currency = 'USD'], [mixed $charset = 'UTF-8'])

For the number for a certain pattern. The valid patterns are 'c', 'd', 'e', 'p' or a custom pattern, such as "#.000" for 3 decimal places.



return:  formatted number string


Parameters:

mixed   $number   the number to format.
string   $pattern   the format pattern, either, 'c', 'd', 'e', 'p' or a custom pattern. E.g. "#.000" will format the number to 3 decimal places.
string   $currency   3-letter ISO 4217 code. For example, the code "USD" represents the US Dollar and "EUR" represents the Euro currency.

[ Top ]

method formatDecimal [line 224]

string formatDecimal( string $string)

Format the decimal places.



return:  formatted decimal places.
access:  protected


Parameters:

string   $string   the decimal number in string form.

[ Top ]

method formatInteger [line 160]

string formatInteger( string $string)

For the integer, perform groupings and string padding.



return:  formatted integer string with grouping
access:  protected


Parameters:

string   $string   the decimal number in string form.

[ Top ]

method setPattern [line 269]

string setPattern( string $pattern)

Set the pattern to format against. The default patterns are retrieved from the NumberFormatInfo instance.



return:  a number format pattern.
access:  protected


Parameters:

string   $pattern   the requested patterns.

[ Top ]


© 2004-2005 PRADO Software Group. All Rights Reserved.