1<?php
2// beware: number_format also rounds
3
4$number = 1234.56;
5
6// english notation (default)
7$english_format_number = number_format($number);
8// 1,235
9
10// French notation
11$nombre_format_francais = number_format($number, 2, ',', ' ');
12// 1 234,56
13
14$number = 1234.5678;
15
16// english notation without thousands separator
17$english_format_number = number_format($number, 2, '.', '');
18// 1234.57
19
1
2<?php
3
4$number = 1234.56;
5
6// let's print the international format for the en_US locale
7setlocale(LC_MONETARY, 'en_US');
8echo money_format('%i', $number) . "\n";
9// USD 1,234.56
10
11// Italian national format with 2 decimals`
12setlocale(LC_MONETARY, 'it_IT');
13echo money_format('%.2n', $number) . "\n";
14// Eu 1.234,56
15
16// Using a negative number
17$number = -1234.5672;
18
19// US national format, using () for negative numbers
20// and 10 digits for left precision
21setlocale(LC_MONETARY, 'en_US');
22echo money_format('%(#10n', $number) . "\n";
23// ($ 1,234.57)
24
25// Similar format as above, adding the use of 2 digits of right
26// precision and '*' as a fill character
27echo money_format('%=*(#10.2n', $number) . "\n";
28// ($********1,234.57)
29
30// Let's justify to the left, with 14 positions of width, 8 digits of
31// left precision, 2 of right precision, without the grouping character
32// and using the international format for the de_DE locale.
33setlocale(LC_MONETARY, 'de_DE');
34echo money_format('%=*^-14#8.2i', 1234.56) . "\n";
35// Eu 1234,56****
36
37// Let's add some blurb before and after the conversion specification
38setlocale(LC_MONETARY, 'en_GB');
39$fmt = 'The final value is %i (after a 10%% discount)';
40echo money_format($fmt, 1234.56) . "\n";
41// The final value is GBP 1,234.56 (after a 10% discount)
42
43?>
44
45