Backend Development 5 min read

Using PHP number_format() Function: Basics, Advanced Techniques, and Real‑World Examples

This article explains how PHP's number_format() function formats numbers with thousands separators, custom decimal points, and currency symbols, providing basic syntax, advanced parameter usage, and practical code examples such as price formatting and handling negative values.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using PHP number_format() Function: Basics, Advanced Techniques, and Real‑World Examples

In PHP programming, handling numbers and strings is common, and when large numbers or thousand separators are needed, the number_format() function becomes essential. This article thoroughly explains the usage of number_format() and provides practical examples to help readers apply the function effectively.

number_format() Function Overview and Basic Usage

The number_format() function formats a number according to a specified pattern and returns the formatted string. Its basic syntax is:

<code>string number_format ( float $number , int $decimals = 0 , string $dec_point = "." , string $thousands_sep = "," )</code>

Where $number is the number to format, $decimals specifies the number of decimal places (default 0), $dec_point defines the decimal point character (default "."), and $thousands_sep defines the thousands separator (default ",").

Simple example:

<code>$number = 1234567.89;
$formatted_number = number_format($number);
echo $formatted_number; // Output: 1,234,568</code>

This formats the number 1234567.89 into the string "1,234,568" .

Advanced Usage of number_format()

Beyond the basic usage, number_format() supports custom decimal points and thousands separators. Example:

<code>$number = 1234567.89;
$formatted_number = number_format($number, 2, ".", "'");
echo $formatted_number; // Output: 1'234'567.89</code>

Here the decimal point is set to "." and the thousands separator to "'", producing "1'234'567.89" .

The function can also handle negative numbers and currency formatting by adjusting $decimals and using the sign of $number . Example:

<code>$number = -1234.5678;
$formatted_number = number_format($number, 2, ".", ",");
echo $formatted_number; // Output: -1,234.57</code>

This keeps two decimal places, uses "," as the thousands separator, and correctly displays the negative sign.

Practical Applications of number_format()

In real‑world development, number_format() is widely used. For instance, an e‑commerce site can format product prices for better readability:

<code>$price = 1999.99;
$formatted_price = number_format($price, 2, ".", ",");
echo "Product price: " . $formatted_price . "元"; // Output: 商品价格:1,999.99元</code>

The function is also useful for formatting large statistical or financial figures in reports, enhancing data visualization.

Conclusion: This article detailed the PHP number_format() function, covering its basic syntax, advanced customization options, and several practical examples. By using number_format() , developers can easily produce readable numeric output, improving the presentation of numbers in web applications.

backend developmentcode examplesString Formattingnumber_format
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.