PHP Formatting Strings

Formatting Strings

PHP provides a number of different functions that can be used to format output in a variety of ways. Some of them are designed to handle special data types—for example, numbers of currency values—while others provide a more generic interface for formatting strings according to more complex rules.

Formatting rules are sometimes governed by locale considerations. For example, most English-speaking countries format numbers by using commas as the separators between thousands, and the point as a separator between the integer portion of a number and its fractional part. In many European countries, this custom is reversed: the dot (or a space) separates thousands, and the comma is the fractional delimiter.

In PHP, the current locale is set by calling the setlocale() function, which takes two parameters: the name of the locale you want to set and a category that indicates which functions are affected by the change. For example, you can affect currency formatting (which we’ll examine in a few paragraphs) to reflect the standard US rules by calling setlocale() as in the following example:

setlocale (LC_MONETARY, ’en_US’);

Formatting Numbers
Number formatting is typically used w hen you wish to output a number and separate its digits into thousands and decimal points. The number_format() function, used for this purpose, is not locale-aware. This means that, even if you have a French or German locale set , it will still use periods for decimals and commas for thousands, unless you specify otherwise.

The number_format() function accepts 1, 2 or 4 arguments (but not three). If only one argument is given, the default formatting is used: the number will be rounded to the nearest integer, and a comma will be used to separate thousands. If two arguments are given, the number will be rounded to the given number of decimal places and a period and comma will be used to separate decimals and thousands, respectively. Should you pass in all four parameters, the number will be rounded to the number of decimal places given, and number_format() will use the first character of the third and fourth arguments as decimal and thousand separators respectively.

Here are a few examples:
echo number_format("100000.698"); // Shows 100,001
echo number_format("100000.698", 3, ",", " "); // Shows 100 000,698

Formatting Currency Values
Currency formatting, unlike number formatting, is locale aware and will display the correct currency symbol (either international or national notations—e.g.: USD or $,respectively) depending on how your locale is set.

When using money_format(), we must specify the formatting rules we want to use by passing the function a specially-crafted string that consists of a percent symbol (%) followed by a set of flags that determine the minimum width of the resulting out-put, its integer and decimal precision and a conversion character that determines whether the currency value is formatted using the locale’s national or international rules.

For example, to output a currency value using the Amer ican national notation with two decimal places, we’d use the following function call:

setlocale(LC_MONETARY, "en_US");
echo money_format(’%.2n’, "100000.698");
This example displays “$100,0 00 .70 ”.

If we simply change the locale to Japanese, we can display the number in Yen.

setlocale(LC_MONETARY, "ja_JP.UTF-8");
echo money_format(’%.2n’, "100000.698");

This time, the output is “¥100,000.70”. Similarly, if we change our formatting to use the i conversion character, money_format() will produce its output using the international notation, for example:

setlocale(LC_MONETARY, "en_US");
echo money_format(’%.2i’, "100000.698");
setlocale(LC_MONETARY, "ja_JP");
echo money_format(’%.2i’, "100000.698");

The first example displays “USD 100,000.70”, while the second outputs “JPY 100,000.70”. As you can see, money_format() is a must for any international commerce site that accepts multiple currencies, as it allows you to easily display amounts in currencies that you are not familiar with.
There are two important things that you should keep in mind here. First, a call to setlocale() affects the entire process inside which it is executed, rather than the individual script. Thus, you should be careful to always reset the locale whenever you need to perform a formatting operation, particularly if your application requires the use of multiple locales, or is hosted alongside other applications that may. In addition, you should keep in mind that the default rounding rules change from locale to locale. For example, US currency values are regularly expressed as dollars and cents, while Japanese currency values are represented as integers. Therefore, if you don’t specify a decimal precision, the same value can yield very different locale-dependent formatted strings:

setlocale(LC_MONETARY, "en_US");
echo money_format(’%i’, "100000.698");
setlocale(LC_MONETARY, "ja_JP");
echo money_format(’%i’, "100000.698");


The first example displays “USD 100,000.70”; however, the Japanese output is now “JPY 100,001”—as you can see, this last value was rounded up to the next integer.

Share this

0 Comment to " PHP Formatting Strings "

Post a Comment