phpeveryday.com

The best tutorial of php, php framework, php strategies, object oriented oriented,


PHP - Number: Formatting Numbers as Currency Values

Tag: number, format number, currency   Category: PHP Basic
post: 16 Mar 2008 read: 190


PHP Number Tips - Part 11: If you want to format a number as per local or international currency conventions, you can define the target locale and then apply the appropriate monetary format via PHP's money_format( ) function such as the following:

<?php
//define currency amount (in INR)
$amount = 10000;

//display in INR
//result: "INR 10000"
setlocale(LC_MONETARY, 'en_IN');
$inr = money_format('%i', $amount);
echo $inr;

//display in US dollars (convert using 1 USD = 45 INR)
//result: "$ 222.22"
setlocale(LC_MONETARY, 'en_US');
$usd = money_format('%n', $amount/45);
echo $usd;

//display in euros (convert using 1 EUR = 52 INR)
//result:
setlocale(LC_MONETARY, 'fr_FR');
$eur = money_format('%i', $amount/52);
echo $eur;
?>

In the above, the listing takes a number and formats it, so it conforms to Indian (INR), American (US), and European (EUR) currency conventions. The set locale( ) function sets the locale and hence conventions for currency displays. Notice that the Indian and American locales differ in their placement of thousand separators while the European locale uses commas instead of decimals. Note that the money_format( ) function is not available in the Windows version of PHP.




| Give Your Opinion | Recommend
Share and Bookmark to: These icons link to social bookmarking sites where readers can share and discover new web pages.
digg del.icio.us technorati Ma.gnolia BlinkList

Recommended articles by other readers:
Web Services: How PHP Kiss VB.NET? (Part 1)
Chart: How to Build Cool Animation Real Time Chart
Joomla: Fast Road to Understand Component Programming
Email: Send Attachement Mail
mod_rewrite - Part 1: create your "fantasy" URL

What do You Think?
Your Name *:
Email *:
(Will not be published)
Website/URL:
Your Comment *:
* Required


615
posting