Formatting Monetary Values

You have a number and you want to print it with thousands and decimal separators.

Use the money_format() function with the %n formating option for a national currency format. For an international format, pass %i.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="EN" lang="EN">
<head>
<title>Formatting Monetary Values</title>
<meta Name="Author" Content="Hann So">
</head>
<body>
<p>
<?php
$num = 12345.67;

echo "<p>The country is US</p>";
setlocale(LC_MONETARY, 'en_US');
echo "format $num in locale: ", money_format('%n', $num), "<br />";
echo "format $num in international: ", money_format('%i', $num), "<br />";

echo "<p>The country is France</p>";
setlocale(LC_MONETARY, 'fr_FR');
echo "format $num in locale: ", money_format('%n', $num), "<br />";
echo "format $num in international: ", money_format('%i', $num), "<br />";

echo "<p>The country is Italy</p>";
setlocale(LC_MONETARY, 'it_IT');
echo "format $num in locale: ", money_format('%n', $num), "<br />";
echo "format $num in international: ", money_format('%i', $num), "<br />";

$num = -12345.67;
echo "<p>The country is US</p>";
setlocale(LC_MONETARY, 'en_US');
echo "format $num in locale: ", money_format('%n', $num), "<br />";
// use ( to wrap negative numbers inside of parentheses
echo "format $num in locale: ", money_format('%(n', $num), "<br />";
// use ! to suppress the currency symbol
echo "format $num in locale: ", money_format('%!n', $num), "<br />";
?>
</p>
</body>
</html>

View the effect


Numbers | Introduction | Checking a Valid Number | Comparing FLoating-Point Numbers | Rounding FLoating-Point Numbers | Series of Integers | Random Numbers | Logarithms | Exponents | Formatting Numbers | Formatting Monetary Values | Printing Correct Plurals | Trigonometric Functions | Converting Between Bases
© 2008: Hann So
email: hso@voyager.deanza.edu