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.

$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)

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

number_format(number, decimals, decimalpoint, separator)

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.

<?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');
?>

Output:

10.00
1,000,000.00
5,458.57
1,254.90
88.96

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:

<?php
echo number_format("1000000", 2, ".", ""); // 1000000.00

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

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

Output:

1000000.00
1x000x000.00
1x000x000y00

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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PHPformattingDecimalnumber_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

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.