25.09.2014 Views

ZEND PHP 5 Certification STUDY GUIDE

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

86 ” Strings And Patterns<br />

This time, the output is “¥100,000.70”. Similarly, if we change our formatting to use<br />

the i conversion character, money_format() will produce its output using the international<br />

notation, for example:<br />

setlocale(LC_MONETARY, "en_US");<br />

echo money_format(’%.2i’, "100000.698");<br />

setlocale(LC_MONETARY, "ja_JP");<br />

echo money_format(’%.2i’, "100000.698");<br />

The first example displays “USD 100,000.70”, while the second outputs “JPY<br />

100,000.70”. As you can see, money_format() is a must for any international commerce<br />

site that accepts multiple currencies, as it allows you to easily display amounts<br />

in currencies that you are not familiar with.<br />

There are two important things that you should keep in mind here. First, a call<br />

to setlocale() affects the entire process inside which it is executed, rather than the<br />

individual script. Thus, you should be careful to always reset the locale whenever<br />

you need to perform a formatting operation, particularly if your application requires<br />

the use of multiple locales, or is hosted alongside other applications that may.<br />

In addition, you should keep in mind that the default rounding rules change from<br />

locale to locale. For example, US currency values are regularly expressed as dollars<br />

and cents, while Japanese currency values are represented as integers. Therefore, if<br />

you don’t specify a decimal precision, the same value can yield very different localedependent<br />

formatted strings:<br />

setlocale(LC_MONETARY, "en_US");<br />

echo money_format(’%i’, "100000.698");<br />

setlocale(LC_MONETARY, "ja_JP");<br />

echo money_format(’%i’, "100000.698");<br />

Licensed to 482634 - Amber Barrow (itsadmin@deakin.edu.au)<br />

The first example displays “USD 100,000.70”; however, the Japanese output is now<br />

“JPY 100,001”—as you can see, this last value was rounded up to the next integer.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!