PHP money_format: The Complete Guide

0
48
PHP money_format() Function Example Tutorial

The money_format() function wraps the C library function strfmon(), with the difference that this implementation converts only one number simultaneously.

PHP money_format

PHP money_format() is a built-in function that returns the string formatted as the currency string. The money_format() function inserts a formatted number where there is a percent (%) sign in the main string.

PHP money_format() function is a predefined function that uses a format number as a currency string. It returns a formatted version of the number. The money_format() function does not work on the Windows platforms.

The money_format() function returns a formatted version of the number

Syntax

See the following syntax.

money_format(string, number)

String parameter

The string parameter is required. Specifies a string to be formatted and how to format the variables in it.

Possible format values

Padding and Flags

  1. =f – Defines the character (f) to be used as padding (Example: %=t. This uses “t” as padding). Default is space
  2. ^ – Removes the use of grouping characters
  3. + or ( – Defines how to show positive and negative numbers. If “+” is used, a local setting for + and – will be used (usually the sign in front of the negative numbers and nothing in front of the positive numbers). If “(” is used, the negative numbers are enclosed in the parenthesis. Default is “+”.
  4. ! – It stops the use of currency symbols in the output string.
  5. – If “-” is used, all fields are left-justified. The default is right-justified.

Field width

  1. x – Defines the minimum field width (x). Default is 0
  2. #x – Defines the maximum number (x) of digits expected to the left of the decimal point. It is used to keep formatted output aligned in the same columns. If the number of digits is more significant than x, this specification is ignored
  3. .x – Defines the maximum number (x) of digits expected to the right of the decimal point. If x is 0, then the decimal point and the digits to its right will not be shown. The default is local settings.

Conversion characters

  1. i – The number is formatted to an international currency format.
  2. n – The number is formatted to a national currency format.
  3. % – It returns the % character.

If multiple format values are used, they must be in the same order as shown above. This function is affected by local settings.

Number parameter

The number parameter is required, and the number is inserted at the %-sign in the format string.

See the following code example.

<?php

$number = 1921.11;
setlocale(LC_MONETARY,"de_DE");
echo money_format("%.2n", $number);

See the output.

➜  pro php app.php
Eu1.921,11                                                                                                   
➜  pro

money_format for US Dollar

Let’s see for the USD.

<?php

$number = 1921.11;
setlocale(LC_MONETARY, 'en_US.UTF-8');
echo money_format('%.2n', $number);

See the output.

➜  pro php app.php
$1,921.11                                                                                                     
➜  pro

If the money_format() function doesn’t seem to be working correctly, make sure you define a valid locale.

Display Currency in Indian Numbering Format In PHP

We can display currency in the Indian numbering format using number_format() in PHP.

<?php

$amount = '1000';
setlocale(LC_MONETARY, 'en_IN');
$amount = money_format('%!i', $amount);
echo $amount;

The above code will work for you.

Double-check that money_format() is defined on any version of PHP you plan your code to run on.  You might be surprised.

That’s it for this tutorial.

Recommended Posts

PHP array_push

PHP array_pop

PHP array_chunk

PHP array to JSON

PHP serialize

Leave A Reply

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.