In PHP, digital formatting is a common operation, especially when displaying currency, percentage, or other data with specific format requirements. Usually, we will use the sprintf() or number_format() function to handle these formatting needs, but these two functions have their own characteristics. How can we cleverly use them to achieve better results?
This article will use examples to show how to format numbers using sprintf() and number_format() .
First, let’s look at the number_format() function. number_format() can format numbers to specified decimal places and can add thousands separators to numbers. This is very useful in financial digital presentation.
$price = 1234567.8910;
$formattedPrice = number_format($price, 2, '.', ',');
echo $formattedPrice;
Output result:
1,234,567.89
In this example, number_format() formats the number $price to retain two decimal places and uses a comma as a thousand separator.
The sprintf() function is more flexible, allowing you to control the precision, width, and even fill characters of the format. For example, if you want to output numbers with a fixed width, or control the number of digits after the decimal point, you can use sprintf() to do so.
$price = 1234567.8910;
$formattedPrice = sprintf("%0.2f", $price);
echo $formattedPrice;
Output result:
1234567.89
In this example, sprintf() is used to format the number into a string that retains two decimal places. The meaning of %0.2f is: output a floating point number and retain two decimal places.
Sometimes we need to use these two functions at the same time to achieve the ideal effect. For example, we might want to keep a number by a certain decimal place and then separate it with a thousand separator. At this time, sprintf() can be used to control the number of decimal places, and number_format() can be used to add a thousand separator.
$price = 1234567.8910;
$formattedPrice = number_format(sprintf("%.2f", $price), 2, '.', ',');
echo $formattedPrice;
Output result:
1,234,567.89
In this example, first use sprintf("%.2f", $price) to ensure that $price retains two decimal places, and then use number_format() to add the thousand separator.
Sometimes we need to embed the formatted numbers into the URL, for example, passing the formatted price as a query parameter to a URL. Here we can also use sprintf() and number_format() to format and embed it into the URL.
$price = 1234567.8910;
$formattedPrice = number_format(sprintf("%.2f", $price), 2, '.', ',');
$url = "https://gitbox.net/checkout?price=" . urlencode($formattedPrice);
echo $url;
Output result:
https://gitbox.net/checkout?price=1%2C234%2C567.89
In this example, sprintf() ensures that the number retains two decimal places, number_format() ensures the correct use of the thousand separator, and urlencode() ensures the correct encoding of the formatted string in the URL.
By combining the sprintf() and number_format() functions, we can flexibly control the format of numbers, whether in the display of financial data or when embedding numbers in URLs. The combination of these two can not only improve the readability of the code, but also make the formatting of numbers more accurate and flexible.
In actual development, selecting the appropriate formatting function combination according to needs will greatly improve the effectiveness and expressiveness of the code.