• Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
logo logo
  • Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
ENEN
  • 简体简体
  • 繁體繁體
Client Area

Php Tip: Use number_format() to format a number with grouped thousands

December 19, 2023

Php Tip: Use number_format() to format a number with grouped thousands

When working with numbers in PHP, it is often necessary to format them in a way that is more readable and visually appealing. One common formatting requirement is to display large numbers with grouped thousands, using commas or periods as separators. In such cases, the number_format() function in PHP comes to the rescue.

What is number_format()?

The number_format() function is a built-in PHP function that allows you to format a number with grouped thousands. It takes a number as its first argument and returns a formatted string representation of that number.

The number_format() function accepts three additional optional arguments:

  • decimals: Specifies the number of decimal places to include in the formatted number. The default value is 0.
  • decimal_point: Specifies the character to use as the decimal point. The default value is a period (.).
  • thousands_separator: Specifies the character to use as the thousands separator. The default value is a comma (,).

Using number_format()

Let’s take a look at some examples to understand how to use the number_format() function:

$number = 1234567.89;
$formatted_number = number_format($number);
echo $formatted_number;
// Output: 1,234,567

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

$number = 1234567.89;
$formatted_number = number_format($number, 2, ',', '.');
echo $formatted_number;
// Output: 1.234.567,89

In the first example, we pass only the number to the number_format() function. It returns a formatted string with grouped thousands using commas as separators.

In the second example, we pass the number, the number of decimal places (2), the decimal point (.), and the thousands separator (,). This results in a formatted string with two decimal places and commas as separators.

In the third example, we pass the number, the number of decimal places (2), the decimal point (,), and the thousands separator (.). This results in a formatted string with two decimal places and periods as separators.

Conclusion

The number_format() function in PHP is a handy tool for formatting numbers with grouped thousands. It allows you to present large numbers in a more readable and visually appealing way. By using the optional arguments, you can customize the number of decimal places, the decimal point, and the thousands separator according to your specific requirements.

Next time you need to format a number with grouped thousands in PHP, remember to use the number_format() function to simplify the task and enhance the user experience.

Summary

In summary, the number_format() function in PHP is a powerful tool for formatting numbers with grouped thousands. It allows you to present large numbers in a more readable and visually appealing way. By using the optional arguments, you can customize the number of decimal places, the decimal point, and the thousands separator according to your specific requirements.

If you are looking for reliable and high-performance VPS hosting solutions, consider Server.HK. With our top-notch VPS hosting services, you can ensure the smooth operation of your website or application.

Recent Posts

  • How to Host a Python Flask or Django Application on Hong Kong VPS (2026)
  • How to Set Up WireGuard VPN on a Hong Kong VPS: Step-by-Step Guide 2026
  • Hong Kong VPS vs DigitalOcean: Cost, Performance, and China Routing Compared (2026)
  • VPS Hosting vs Shared Hosting: Why the Upgrade Is Worth It for Asia-Facing Websites
  • Hong Kong VPS vs Google Cloud Asia: Which Delivers Better China Performance in 2026?

Recent Comments

  1. vibramycin injection on How to Choose the Right Hong Kong VPS Plan: A Buyer’s Guide for 2026
  2. allopurinol for gout on CN2 GIA vs BGP vs CN2 GT: What’s the Real Difference for China Connectivity?
  3. antibiotics online purchase on How to Set Up a WordPress Site on a Hong Kong VPS with aaPanel (Step-by-Step 2026)
  4. linezolid cost oral on Top 5 Use Cases for a Hong Kong Dedicated Server in 2026
  5. metoprolol generic on Hong Kong VPS vs Japan VPS: Head-to-Head for Asia-Pacific Deployments in 2026

Knowledge Base

Access detailed guides, tutorials, and resources.

Live Chat

Get instant help 24/7 from our support team.

Send Ticket

Our team typically responds within 10 minutes.

logo
Alipay Cc-paypal Cc-stripe Cc-visa Cc-mastercard Bitcoin
Cloud VPS
  • Hong Kong VPS
  • US VPS
Dedicated Servers
  • Hong Kong Servers
  • US Servers
  • Singapore Servers
  • Japan Servers
More
  • Contact Us
  • Blog
  • Legal
© 2026 Server.HK | Hosting Limited, Hong Kong | Company Registration No. 77008912
Telegram
Telegram @ServerHKBot