InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Localizing Currency Values in PHP
PHP

Localizing Currency Values in PHP

InfinityCoder December 21, 2016

You want to display currency amounts in a locale-specific format.

For default formatting inside a message, use the currency style of the number argument type:

1
2
3
4
5
6
7
$income = 5549.3;
$debit = -25.95;
 
$fmt = new MessageFormatter('en_US',
                           {0,number,currency} in and {1,number,currency} out');
 
print $fmt->format(array($income,$debit));

This prints:

1
$5,549.30 in and -$25.95 out

For more specific formatting, use the formatCurrency() method of a NumberFormatter:

1
2
3
4
5
6
$income = 5549.3;
$debit = -25.95;
 
$fmt = new NumberFormatter('en_US', NumberFormatter::CURRENCY);
print $fmt->formatCurrency($income, 'USD') . ' in and ' .
  $fmt->formatCurrency($debit, 'EUR') . ' out';

This prints:

1
$5,549.30 in and -€25.95 out

The currency style of the number argument type in MessageFormatter uses the default currency and formatting rules for the locale of the MessageFormatter instance.

This is certainly a concise and easy way to include local currency amounts in messages you are producing. The code that uses MessageFormatter prints:

1
$5,549.30 in and -$25.95 out

The formatCurrency() method of NumberFormatter makes it easy to specify other currencies.

In the example that uses NumberFormatter, because the first call to format Currency() specifies USD (for US dollars) as the currency and the second specifies EUR (for Euro), the code prints:

1
$5,549.30 in and -€25.95 out

Although you can construct complex currency formatting rules with the decimal format patterns that MessageFormatter understands, it is often clearer to express those needs via the programmatic interface NumberFormatter provides. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$amounts = array( array(152.9, 'USD'),
                  array(328, 'ISK'),
                  array(-1, 'USD'),
                  array(500.53, 'EUR') );
 
$fmt = new NumberFormatter('en_US', NumberFormatter::CURRENCY);
$fmt->setAttribute(NumberFormatter::PADDING_POSITION,
                   NumberFormatter::PAD_AFTER_PREFIX);
$fmt->setAttribute(NumberFormatter::FORMAT_WIDTH, 15);
$fmt->setTextAttribute(NumberFormatter::PADDING_CHARACTER, ' ');
 
foreach ($amounts as $amount) {
   print $fmt->formatCurrency($amount[0], $amount[1]) . "\n";
}

This prints out a table of four values in different currencies, inserting enough padding between the currency symbol and the value to make each line 15 characters wide.

The padding character used is a space, not the default of *. This displays:

1
2
3
4
$    152.90
ISK  328
-$   1.00
€    500.53

 

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Querying an SQL Database in PHP
You want to retrieve some data from your database. Use …

Querying an SQL Database in PHP

Accessing an Object Using Array Syntax in PHP
You have an object, but you want to be able …

Accessing an Object Using Array Syntax in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap