Backend Development 5 min read

How to Keep Two Decimal Places in PHP Using round(), sprintf() and number_format()

This tutorial demonstrates three PHP techniques—round(), sprintf(), and number_format()—to format numbers with exactly two decimal places, explains the number_format() function syntax and parameters, and provides practical examples for displaying product prices with customizable decimal points and thousand separators.

php中文网 Courses
php中文网 Courses
php中文网 Courses
How to Keep Two Decimal Places in PHP Using round(), sprintf() and number_format()

The article explains how to retain two decimal places in PHP, presenting three common methods: using round() for rounding, sprintf() for string formatting, and number_format() for number formatting with optional thousand separators.

<code>$num = 10.4567;
// Method 1: round()
echo round($num, 2); // 10.46

// Method 2: sprintf()
$format_num = sprintf("%.2f", $num);
echo $format_num; // 10.46

// Method 3: number_format()
echo number_format($num, 2); // 10.46
// Alternative call
echo number_format($num, 2, '.', ''); // 10.46 (no thousands separator)</code>

It then introduces the number_format() function, which can format numbers with thousands grouping. The syntax is:

<code>number_format(number, decimals, decimalpoint, separator)</code>

Parameters:

number : required, the number to format.

decimals : optional, number of decimal digits.

decimalpoint : optional, string used as decimal point.

separator : optional, string used as thousands separator.

Example: formatting product prices in yuan with two decimal places.

<code>&lt;?php
$a = 10;
echo number_format($a, '2');

$b = 1000000;
echo number_format($b, '2');

$c = 5458.5684;
echo number_format($c, '2');

$d = '1254.8963';
echo number_format($d, '2');

$e = '88.9643';
echo number_format($e, '2');
?&gt;</code>

Output:

<code>10.00
1,000,000.00
5,458.57
1,254.90
88.96</code>

Key observations:

Both numeric and string representations of numbers can be processed by number_format() .

If the integer part lacks decimals, the function pads with zeros when a decimal count is specified.

When decimals are present, the function rounds the value to the nearest digit.

Without specifying the third and fourth arguments, the function inserts commas as thousands separators for numbers with more than three digits.

Customizing separators:

<code>&lt;?php
echo number_format("1000000", 2, ".", ""); // 1000000.00

echo number_format("1000000", 2, ".", "x"); // 1x000x000.00

echo number_format("1000000", 2, "y", "x"); // 1x000x000y00
?&gt;</code>

Output:

<code>1000000.00
1x000x000.00
1x000x000y00</code>

Additional notes: the third and fourth parameters of number_format() must be used together; they allow changing the decimal point character and the thousands separator respectively.

Finally, the article lists several recommended PHP learning resources, such as Vue3+Laravel8+Uniapp tutorials, Vue3+TP6+API e‑commerce development courses, Swoole training, and Workerman+TP6 instant‑messaging system tutorials.

backendPHPformattingDecimalnumber_formatroundsprintf
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.