July 13th, 2000, 09:54 AM
Not sure if this is a PHP post or HTML...
I want to display currency values to 2 decimal places (£145.00) on a page generated by php. If the value is returned directly from a double database vale it displays ok, however when I perform any arithmetic on a number it will only display £145.
Is the result being treated as a string?
July 13th, 2000, 10:45 AM
$fmt_price = sprintf("%0.2f",$price);
echo "<P>The price is $$fmt_price.</p>";
From the day we're born, we're running out of time.
July 13th, 2000, 10:46 AM
July 13th, 2000, 10:15 PM
you can also use number_format().
string number_format (float number, int decimals, string dec_point, string thousands_sep)
number_format() returns a formatted version of number. This function accepts either one, two or four parameters (not three):
If only one parameter is given, number will be formatted without decimals, but with a comma (",") between every group of thousands.
If two parameters are given, number will be formatted with decimals decimals with a dot (".") in front, and a comma (",") between every
group of thousands.
If all four parameters are given, number will be formatted with decimals decimals, dec_point instead of a dot (".") before the decimals and
thousands_sep instead of a comma (",") between every group of thousands.