To show a number with two decimals, use number_format($x, 2): it returns a string such as "1,234,567.89" and keeps trailing zeros (1.5 becomes "1.50"). Pass '.' and '' as the last two arguments to drop the comma. To round the value itself for further math, use round($x, 2), which returns a float and prints 1.5, not 1.50. For leading zeros, use sprintf('%05d', $n).
"Two decimal places" means two different jobs in PHP. Rounding the number, so later math uses 3.14 instead of 3.14159, is round(). Showing it with exactly two digits, zeros included, is number_format() or sprintf(), and those return strings. The sections below cover all three, why round() and sprintf() disagree on 2.675, the rounding modes (including PHP 8.4's RoundingMode enum and how to always round up or down), money in integer cents, and padding numbers with leading zeros, then a table to pick from. Each example runs on this page: hit Run, then edit the code and run it again.
1number_format() for displayRecommended
number_format($num, $decimals, $decimalSeparator, $thousandsSeparator) rounds to the number of decimals you ask for and returns a formatted string. With only the first two arguments it uses a dot for decimals and a comma between thousands. Pass the last two to change them: an empty string removes the thousands separator, and a comma plus a dot gives the European style. Use it at the point where a number becomes text, in a template, a CSV or a JSON field that must show two decimals, and keep the number itself as an int or float until then.
<?php $price = 1234567.891; // Two decimals with the default separators: "." and "," echo number_format($price, 2), "\n"; // No thousands separator: pass '' as the fourth argument echo number_format($price, 2, '.', ''), "\n"; // European style: comma for decimals, dot for thousands echo number_format($price, 2, ',', '.'), "\n"; // It pads with zeros and always returns a string var_dump(number_format(1.5, 2)); var_dump(number_format(7, 2)); // Leave out the decimals and it rounds to a whole number echo number_format(2.5), "\n"; // A tiny negative value does not print as -0.00 echo number_format(-0.001, 2), "\n";
Output
The same value prints as 1,234,567.89, 1234567.89 and 1.234.567,89 depending on the separators. var_dump shows the result is a string, string(4) "1.50", with the trailing zero that a float can't keep, and an int works too ("7.00"). Without a decimals argument, 2.5 rounds to 3: halves go away from zero. -0.001 prints 0.00, with no stray minus sign. Because the result is a string with commas, don't do math with it: "1,234.50" + 1 reads only the 1 before the comma.
2round() to keep a number
round($num, $precision) returns a float rounded to $precision digits after the point. Use it when the rounded value goes back into a calculation, a comparison or a database column. It can't give you "1.50": a float has no idea how many zeros you want, so PHP prints the shortest form, 1.5. When you need both, round to compute and format to display. A negative precision rounds to the left of the point, to tens or hundreds.
<?php $x = 3.14159; $rounded = round($x, 2); var_dump($rounded); echo $rounded * 2, "\n"; // round() returns a float, so trailing zeros are gone echo round(1.5, 2), "\n"; echo round(7, 2), "\n"; var_dump(round(7, 2)); // Halves round away from zero by default echo round(2.675, 2), ' ', round(-2.675, 2), "\n"; // A negative precision rounds to tens, hundreds, ... echo round(1234.5678, -2), "\n"; // Round to compute, then format to display $total = round(19.999 * 3, 2); echo number_format($total, 2), "\n";
Output
round(3.14159, 2) is float(3.14), a number you can keep calculating with (6.28). round(1.5, 2) prints 1.5 and round(7, 2) prints 7, and even an int comes back as float(7): this is the "why doesn't it show two decimals" question, and the answer is to format the result. The default mode rounds halves away from zero, so 2.675 and -2.675 give 2.68 and -2.68. The last line rounds 59.997 to 60 and lets number_format put back the zeros: 60.00.
3sprintf('%.2f') and printf
sprintf('%.2f', $x) returns the number as a string with exactly two decimals, and printf prints it directly. It adds no thousands separator, but the format string can also set a width, alignment and a + sign, which makes it the tool for lining up columns or building a message around the number. It does have one difference from round() and number_format() that surprises people, shown below.
<?php $x = 3.14159; // sprintf returns the string, printf prints it $s = sprintf('%.2f', $x); var_dump($s); printf("Total: %.2f EUR\n", 1.5); // Width, sign and padding in the same format printf("[%8.2f] [%-8.2f] [%+.2f]\n", $x, $x, $x); // The surprise: round() and sprintf() disagree on 2.675 echo round(2.675, 2), "\n"; printf("%.2f\n", 2.675); // 2.675 cannot be stored exactly; here is the real value printf("%.20f\n", 2.675); // Unlike number_format, sprintf keeps the minus sign echo sprintf('%.2f', -0.001), ' ', number_format(-0.001, 2), "\n";
Output
%8.2f right-aligns in eight characters, %-8.2f left-aligns, and %+.2f adds the sign. Then the surprise: round(2.675, 2) gives 2.68 while printf("%.2f") gives 2.67. A float is stored in binary, and the closest it can get to 2.675 is 2.67499999999999982236, just under the halfway point. sprintf rounds that stored value, so it goes down. round() (and number_format(), which rounds the same way) corrects for that tiny representation error first and rounds the decimal you wrote, so it goes up. Neither is broken; if every cent must be predictable, keep money out of floats (section 5). Last line: sprintf prints -0.00 where number_format prints 0.00.
4Rounding modes, and always rounding up or down
round() takes a third argument, the mode. The four PHP_ROUND_* constants decide only what happens to an exact half: HALF_UP (the default, away from zero), HALF_DOWN (towards zero), HALF_EVEN (banker's rounding, to the even digit) and HALF_ODD. PHP 8.4 added the RoundingMode enum, which has the same four as HalfAwayFromZero, HalfTowardsZero, HalfEven and HalfOdd, plus four that always go one way: PositiveInfinity (up), NegativeInfinity (down), TowardsZero (truncate) and AwayFromZero. Those replace the old ceil($x * 100) / 100 trick, which fails more often than people expect.
<?php // The four classic modes, as PHP_ROUND_* constants $modes = [ 'HALF_UP' => PHP_ROUND_HALF_UP, 'HALF_DOWN' => PHP_ROUND_HALF_DOWN, 'HALF_EVEN' => PHP_ROUND_HALF_EVEN, 'HALF_ODD' => PHP_ROUND_HALF_ODD, ]; foreach ($modes as $name => $mode) { printf( "%-9s 2.125 -> %.2f 2.135 -> %.2f -2.125 -> %.2f\n", $name, round(2.125, 2, $mode), round(2.135, 2, $mode), round(-2.125, 2, $mode), ); } // PHP 8.4: the RoundingMode enum, with modes that ignore the halfway rule echo round(2.125, 2, RoundingMode::HalfEven), "\n"; echo round(2.341, 2, RoundingMode::PositiveInfinity), "\n"; // always up echo round(2.349, 2, RoundingMode::NegativeInfinity), "\n"; // always down echo round(-2.349, 2, RoundingMode::TowardsZero), "\n"; // truncate // The old trick for "always up" breaks on float error var_dump(1.1 * 100); echo ceil(1.1 * 100) / 100, "\n"; echo floor(0.29 * 100) / 100, "\n"; // The enum gets these right echo round(1.1, 2, RoundingMode::PositiveInfinity), "\n"; echo round(0.29, 2, RoundingMode::NegativeInfinity), "\n"; // Before PHP 8.4: clean up the product before ceil/floor echo ceil(round(1.1 * 100, 6)) / 100, "\n"; echo floor(round(0.29 * 100, 6)) / 100, "\n";
Output
On a half the modes split: HALF_EVEN sends 2.125 to 2.12 and 2.135 to 2.14, always landing on an even last digit, which is why it is used for statistics and some accounting rules. The one-way modes give 2.35 for 2.341 rounded up and 2.34 for 2.349 rounded down. The old trick fails because 1.1 * 100 is 110.00000000000001, so ceil turns 1.1 into 1.11, and floor turns 0.29 into 0.28 the same way. RoundingMode returns 1.1 and 0.29; on PHP before 8.4, round the product to a few decimals before calling ceil or floor, as the last two lines do. The PHP_ROUND_* constants still work in PHP 8.4 and later.
5Money: work in integer cents
Floats can't hold most decimal fractions exactly, so sums of prices pick up tiny errors, and an equality check or a value that lands near a half can go the wrong way even after rounding. The usual fix in PHP is to store amounts as an int number of cents (or the currency's smallest unit), do all the math on ints, round once where a percentage creates a fraction of a cent, and divide by 100 only to print. Integer addition is exact, and intdiv plus the remainder splits a bill without losing a cent.
<?php // Floats drift: ten payments of 0.10 do not add up to 1.00 $sum = 0.0; for ($i = 0; $i < 10; $i++) { $sum += 0.1; } var_dump($sum, $sum == 1.0); // Store money as whole cents in an int; the sums are exact $prices = [1999, 450, 1]; // 19.99, 4.50, 0.01 $subtotal = array_sum($prices); // Tax at 8.25%: round to whole cents once, right here $tax = (int) round($subtotal * 825 / 10000); $total = $subtotal + $tax; function money(int $cents): string { return number_format($cents / 100, 2); } echo 'Subtotal: ', money($subtotal), "\n"; echo 'Tax: ', money($tax), "\n"; echo 'Total: ', money($total), "\n"; // Split 100.00 three ways without losing a cent $bill = 10000; $share = intdiv($bill, 3); $shares = array_fill(0, 3, $share); $shares[0] += $bill - $share * 3; // first person pays the leftover cent echo implode(' + ', array_map(money(...), $shares)), ' = ', money(array_sum($shares)), "\n";
Output
Adding 0.1 ten times gives float(0.9999999999999999), and the comparison with 1.0 is bool(false). In cents nothing drifts: the subtotal is exactly 24.50, the tax of 202.125 cents is rounded once to 2.02, and the split prints 33.34 + 33.33 + 33.33 = 100.00. If you need decimal arithmetic on strings instead, the bcmath extension (bcadd, bcmul, and the BcMath\Number class in PHP 8.4) or a money library does that; bcmath is not on this page's engine. For currency symbols and locale-specific formats, NumberFormatter needs the intl extension, which isn't here either.
6Leading zeros
The other formatting question: 42 must print as 00042. Use sprintf with a 0 flag and a width: %05d pads an integer to five characters. The width is a minimum, so a longer number is never cut, and it counts the minus sign, which stays in front of the zeros. For a decimal, %06.2f combines padding with two decimal places. str_pad also pads, but it treats the value as plain text, so use it for codes that are already strings, not for signed numbers.
<?php // Pad an integer to 5 digits echo sprintf('%05d', 42), "\n"; // The width counts the minus sign, which stays in front echo sprintf('%05d', -42), "\n"; // Longer numbers are never cut echo sprintf('%05d', 1234567), "\n"; // Zero-padded decimals: the 6 is the total width, dot included echo sprintf('%06.2f', 3.14159), "\n"; // str_pad works on strings and knows nothing about signs echo str_pad('42', 5, '0', STR_PAD_LEFT), "\n"; echo str_pad('-42', 5, '0', STR_PAD_LEFT), "\n"; // Typical uses: invoice numbers, times, ISO dates printf("INV-%06d\n", 987); printf("%02d:%02d\n", 7, 5); printf("%04d-%02d-%02d\n", 2026, 3, 9); // Reading it back: (int) drops the leading zeros var_dump((int) '00042');
Output
%05d turns 42 into 00042 and -42 into -0042, five characters with the sign first, and leaves 1234567 whole. %06.2f gives 003.14: six characters in total. str_pad matches for "42" but turns "-42" into 00-42, which is why sprintf is the safer default for numbers. Keep padded values as strings: casting "00042" back with (int) gives int(42). For dates, date('Y-m-d') pads on its own; see formatting a date.
7Which should you use?
| Method | Returns | 2.675 gives | Best for |
|---|---|---|---|
| number_format($x, 2) | String | "2.68" | Showing a number, with thousands separators |
| round($x, 2) | Float | 2.68 | A rounded value for more math (prints 1.5, not 1.50) |
| sprintf('%.2f', $x) | String | "2.67" | Messages, widths and aligned columns |
| round($x, 2, RoundingMode::...) | Float | Depends on the mode | Half-even, always up or always down (PHP 8.4+) |
| ceil($x * 100) / 100 | Float | 2.68 | Avoid: 1.1 becomes 1.11 |
| int cents | Int | No float to round | Money: exact sums and splits |
| sprintf('%05d', $n) | String | n/a | Leading zeros, negative numbers included |
Frequently asked questions
Why does round() show 1.5 instead of 1.50 in PHP?
round() returns a float, and a float does not remember trailing zeros, so echo round(1.5, 2) prints 1.5 and round(7, 2) prints 7. Rounding and display are separate steps: keep the float for math, and turn it into text with number_format($x, 2) or sprintf('%.2f', $x), which both print 1.50.
Why does round(2.675, 2) give 2.68 but sprintf('%.2f', 2.675) give 2.67?
The float closest to 2.675 is 2.67499999999999982236, slightly below the halfway point. sprintf rounds that stored value and gets 2.67. round() and number_format() correct for the tiny representation error and round the decimal you wrote, giving 2.68. For amounts where every cent matters, use integer cents instead of floats.
How do I remove the comma from number_format in PHP?
Pass the decimal separator and an empty thousands separator: number_format(1234567.891, 2, '.', '') returns 1234567.89. With only two arguments, the thousands separator is a comma. The same two arguments give other styles, such as number_format($x, 2, ',', '.') for 1.234.567,89.
How do I always round up to 2 decimal places in PHP?
On PHP 8.4 and later, use round($x, 2, RoundingMode::PositiveInfinity), or RoundingMode::NegativeInfinity to always round down. The common ceil($x * 100) / 100 breaks on float error: 1.1 * 100 is 110.00000000000001, so it returns 1.11. On older PHP, round the product first: ceil(round($x * 100, 6)) / 100.
Can I do math with the result of number_format?
No. number_format returns a string, often with a thousands separator, and PHP reads only the leading digits of it: "1,234.50" + 1 gives 2 and a Warning: A non-numeric value encountered. Do the math on the number and call number_format last, when you print.
How do I add leading zeros to a number in PHP?
Use sprintf('%05d', $n): 42 becomes 00042 and -42 becomes -0042, with the sign kept in front. For a decimal, sprintf('%06.2f', 3.14159) gives 003.14. str_pad((string) $n, 5, '0', STR_PAD_LEFT) also works for non-negative values, but turns -42 into 00-42.
How do I format a number as currency in PHP?
For a fixed format, combine a symbol with number_format, for example '$' . number_format($cents / 100, 2). For locale-aware output, with the symbol and separators each country expects, use NumberFormatter with the NumberFormatter::CURRENCY style and its formatCurrency method; it needs the intl extension, which this page's engine does not load.