PHP number format without comma

PHP number format without comma

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Controlling Decimal Formatting in PHP Without Thousands Separators

The default behavior of number_format() in PHP is convenient for financial reports that require locale-aware grouping, but it often complicates simple display tasks. When you call number_format(1000.5, 2), the function returns "1,000.50". This includes a thousands separator (comma), which might be necessary for readability in some contexts but is strictly unwanted when generating code snippets, API responses, or specific UI elements.

To achieve the output 1000.50—ensuring exactly two decimal places while stripping the comma—you must explicitly control the third parameter of the function: the dec_point. However, simply setting that parameter to an empty string is not enough if you also want to remove the thousands separator entirely. The most direct approach is passing an empty string as the decimal point character and ensuring no grouping separator is defined.

Here is the corrected implementation:

$formatted = number_format(1000.5, 2, '.', '');
// Result: "1000.5" (Wait, this still groups by thousands if locale allows, or just removes decimal?)

Actually, let's refine that. The signature for number_format() is number_format(number, decimals, dec_point, thousands_sep). To get exactly 1000.50 with no comma at all, you need to pass an empty string for the thousands_sep argument.

$formatted = number_format(1000.5, 2, '.', ''); 
// Output: "1000.5" -> Wait, does it group?

Let's re-evaluate the behavior based on standard PHP documentation. The fourth parameter (thousands_sep) defaults to , (or the locale-specific separator). If you pass an empty string '', you effectively tell PHP not to insert any character between thousands.

$formatted = number_format(1000.5, 2, '.', ''); 
// Output: "1000.5"

Wait, the source material says number_format(1000.5, 2) results in 1,000.50. This implies the default decimal point is . and the default thousands separator is ,. If we want to remove the thousands separator, we pass an empty string for the fourth argument.

Correct code:

$formatted = number_format(1000.5, 2, '.', '');
// Output: "1000.5" 

Hold on, does number_format preserve trailing zeros if the decimals parameter is set? Yes. So number_format(1000.5, 2) gives 1,000.50. If we pass an empty string for the thousands separator, it should become 1000.50.

Let's verify the logic again.
Input: 1000.5
Decimals: 2 -> Forces .50
Decimal Point: . (default)
Thousands Sep: , (default) -> We want this empty.

So the call is:

$formatted = number_format(1000.5, 2, '.', '');

This yields "1000.50". This matches the requirement.

Why This Works

The number_format() function accepts four arguments. The first three are often sufficient, but the fourth is critical here. By passing an empty string '' as the thousands separator, you instruct the engine to skip the grouping logic entirely. Without this explicit argument, PHP falls back to the default behavior, which inserts a comma (or locale-specific equivalent) after every three digits from the right.

Handling Locale Sensitivity

While passing an empty string works in most standard environments, be aware that number_format() respects the current locale settings (LC_NUMERIC). In some configurations, even if you don't pass the separator, PHP might try to use a space or a period depending on your system's environment variables. If you are building a library or an API response where consistency is paramount across different server environments, it is safer to explicitly define both the decimal point and the thousands separator.

// Explicitly defining both ensures consistent output regardless of locale
$formatted = number_format(1000.5, 2, '.', ''); 

Alternative: Formatting with sprintf or printf

If you prefer not to rely on number_format() or need more granular control over the string format without the overhead of thousands separators, sprintf() is a robust alternative. You can use the 'f' format specifier combined with precision.

// Using sprintf for fixed-point notation
$formatted = sprintf('%.2f', 1000.5);
// Output: "1000.50"

This approach has a distinct advantage: it does not apply thousands separators by default. However, unlike number_format(), sprintf() does not automatically add a trailing zero if the number is an integer with no decimal part (e.g., sprintf('%.2f', 100) becomes "100.00", which is what we want). Wait, sprintf with %.2f always outputs two decimal places. So sprintf('%.2f', 100) results in "100.00". This is often the cleaner solution when thousands separators are not desired at all.

Trade-offs and Considerations

Using number_format() with an empty thousands separator is generally preferred when you want to keep the function's flexibility for decimal point customization (e.g., using a comma as a decimal point in European locales). In that case, you might do:

// European style: 1.000,50
$formatted = number_format(1000.5, 2, ',', '.'); 

If your goal is strictly to strip grouping characters regardless of locale, sprintf offers a more predictable output without needing to manage the fourth argument of number_format(). Just ensure you are aware that sprintf formats numbers as strings, so remember to cast back to float if further arithmetic is required on the result.

In summary, to display 1000.5 as 1000.50 without any thousands separators in PHP, pass an empty string for the thousands separator argument in number_format(), or simply use sprintf('%.2f', $number) for a straightforward, separator-free fixed-point format.

Our network