Alternatives to printf()
Although the printf() function is extremely useful, it is also computationally intensive. As a result, you should try to limit its use as much as possible, relying instead on other functions provided by PHP for more specific tasks.
For example, you can use the number_format function to format a number according to a number of parameters:
number_format ( $number, [$decimals, [$point_separator, $thousand_separator]] );
The function works by formatting $number using at a minimum $decimals decimal digits, using $point_separator as a separator between the integer and decimal parts, and $thousand_separator to separate groups of thousands. If $decimals isn't specified, no decimal digits are shown. If $point_separator and $thousand_separator aren't used, the interpreter uses a dot (.) and a comma (,) in their place.
For example, in countries such as the U.K. and the United States, numbers are formatted using commas to separate the thousand groups, and dots are used to separate the integer part from the decimal part. Some European countries, such as Italy, use the opposite notation: dots separate the thousands and the comma indicates the beginning of the decimal part. Here's how number_format can be used to satisfy both requirements:
<?php
$a = 1232322210.44;
echo number_format ($a, 2); // English format echo "\n"; echo number_format ($a, 2, ',', '.'); // Italian format echo "\n";
?>
The preceding example produces the following output:
1,232,322,210.44 1.232.322.210,44
Tags:
Add more tags...,
|