How7o
  • Home
  • Marketing
    MarketingShow More
  • OS
    OSShow More
    How to force quit frozen apps in Ubuntu
    Force Close an App in Ubuntu (xkill, System Monitor, kill -9)
    4 Min Read
  • Features
    FeaturesShow More
  • Guide
    GuideShow More
  • Contact
  • Blog
Reading: How to Add Commas to Numbers in PHP (Thousands Separator)
Share
Subscribe Now
How7oHow7o
Font ResizerAa
  • Marketing
  • OS
  • Features
  • Guide
  • Complaint
  • Advertise
Search
  • Categories
    • Marketing
    • OS
    • Features
    • Guide
    • Lifestyle
    • Wellness
    • Healthy
    • Nutrition
  • More Foxiz
    • Blog Index
    • Complaint
    • Sitemap
    • Advertise
Follow US
Copyright © 2014-2023 Ruby Theme Ltd. All Rights Reserved.
How7o > Blog > Uncategorized > How to Add Commas to Numbers in PHP (Thousands Separator)
Uncategorized

How to Add Commas to Numbers in PHP (Thousands Separator)

how7o
By how7o
Last updated: January 30, 2026
5 Min Read
Add commas to numbers in PHP using number_format()
SHARE

I ran into a small-but-annoying issue while displaying prices: I needed to add commas to numbers in PHP so amounts like 22222.22 show up as 22,222.22. It’s one of those details that makes your UI feel “finished,” especially for currency values.

Contents
  • What I needed (and why it matters)
  • The simplest solution: PHP number_format()
  • How number_format() works (so you can adapt it)
  • Step-by-step: format any number with commas in PHP
    • Step 1: Make sure you’re working with a numeric value
    • Step 2: Choose decimal places
    • Step 3: Apply number_format() and display it
  • Formatting currency cleanly (with a dollar sign)
  • Troubleshooting (common gotchas)
    • 1) My number includes a currency sign (like 22222.22$)
    • 2) I’m seeing rounding I didn’t expect
    • 3) The separators look wrong for my region
    • 4) Large money values need precision
  • Official docs (recommended references)
  • Related guides

The good news: PHP already ships with a built-in function for this. Here’s how I solved it, plus a handful of examples (including different notations) so you can copy/paste what you need.

What I needed (and why it matters)

I had a number like 22222.22 and wanted to display it as 22,222.22 (thousands separator + fixed decimals). If you’re printing totals, invoices, product prices, or reports, formatting like this improves readability and reduces mistakes.

The simplest solution: PHP number_format()

Use number_format(). It formats a number with grouped thousands and (optionally) a fixed number of decimal places.

<?php
$number = 22222.22;

// 22,222.22
echo number_format($number, 2, '.', ',');
?>

That’s the exact output you’re after: 22,222.22. If you’re displaying dollars, you’d typically add the symbol separately (more on that below).

How number_format() works (so you can adapt it)

The function signature looks like this:

number_format(float $num, int $decimals = 0, string $decimal_separator = ".", string $thousands_separator = ",")
  • $num: your number
  • $decimals: how many digits after the decimal point (e.g., 2 for money)
  • $decimal_separator: usually . in US/UK notation
  • $thousands_separator: usually , in US/UK notation

Step-by-step: format any number with commas in PHP

Here’s the repeatable process I use whenever I need to add commas to numbers in PHP.

Step 1: Make sure you’re working with a numeric value

Store your number as an int or float (not as a string like "22222.22$"). If you have symbols attached, strip them before formatting.

Step 2: Choose decimal places

For currency, I usually want 2 decimal places. For counts (like users, views), I often want 0.

Step 3: Apply number_format() and display it

<?php
$number = 22222.2222;

// Add thousands separator, no decimals: 22,222
echo number_format($number);

// Keep 2 decimals + thousands separator: 22,222.22
echo number_format($number, 2);

// Keep 2 decimals, remove thousands separator: 22222.22
echo number_format($number, 2, '.', '');

// French-style: space thousands, comma decimals: 22 222,22
echo number_format($number, 2, ',', ' ');

// Vietnamese-style: dot thousands, comma decimals: 22.222,22
echo number_format($number, 2, ',', '.');
?>

Formatting currency cleanly (with a dollar sign)

If you want 22,222.22$ or $22,222.22, format the number first, then add the symbol. This avoids mixing currency symbols into your numeric value (which causes bugs later).

<?php
$amount = 22222.22;

echo '$' . number_format($amount, 2, '.', ',');   // $22,222.22
echo number_format($amount, 2, '.', ',') . '$';   // 22,222.22$
?>

Troubleshooting (common gotchas)

1) My number includes a currency sign (like 22222.22$)

That’s not a number anymore—it’s a string. Remove non-numeric characters before formatting.

<?php
$raw = "22222.22$";

// keep digits, minus sign, and dot
$clean = preg_replace('/[^0-9.\-]/', '', $raw);

echo number_format((float) $clean, 2, '.', ','); // 22,222.22
?>

2) I’m seeing rounding I didn’t expect

number_format() rounds based on the decimals you request. If you pass 2, it will round to 2 decimal places. That’s usually correct for prices.

3) The separators look wrong for my region

Use the 3rd and 4th parameters to define decimal and thousands separators. For example, many European formats use comma decimals and spaces/dots for thousands.

4) Large money values need precision

For everyday display formatting, number_format() is fine. But if you’re doing calculations with money, use integer cents (e.g., store 2222222 cents) or a decimal-safe approach before formatting the final output.

Official docs (recommended references)

  • PHP manual: number_format()
  • PHP manual: PCRE (preg_replace)

Related guides

(Add your internal links here — send me 1–3 URLs and I’ll plug them into this section.)

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Copy Link Print
Previous Article Set a variable in Laravel Blade using the @php directive How to Set a Variable in Laravel Blade Templates (With Examples)
Leave a Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

FacebookLike
XFollow
PinterestPin
InstagramFollow

Subscribe Now

Subscribe to our newsletter to get our newest articles instantly!
Most Popular
Add commas to numbers in PHP using number_format()
How to Add Commas to Numbers in PHP (Thousands Separator)
January 30, 2026
Set a variable in Laravel Blade using the @php directive
How to Set a Variable in Laravel Blade Templates (With Examples)
January 30, 2026
CSS print styles shown in a clean print preview layout
How to Add CSS Print Styles for Printer and Print Screen
January 30, 2026
CyberPanel too many redirects Laravel error fixed by restarting LiteSpeed
Fix CyberPanel Too Many Redirects (ERR_TOO_MANY_REDIRECTS) for Laravel Subdomain
January 23, 2026
How I Fixed Composer Dependency Errors
How I Fixed Composer Dependency Errors Using the –ignore-platform-reqs Flag (Step-by-Step Guide)
January 12, 2026

Always Stay Up to Date

Subscribe to our newsletter to get our newest articles instantly!
How7o

We provide tips, tricks, and advice for improving websites and doing better search.

Latest News

  • SEO Audit Tool
  • Client ReferralsNew
  • Execution of SEO
  • Reporting Tool

Resouce

  • Google Search Console
  • Google Keyword Planner
  • Google OptimiseHot
  • SEO Spider

Get the Top 10 in Search!

Looking for a trustworthy service to optimize the company website?
Request a Quote
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?