Encodera

Thanks for reading!

Donate to the site — we work hard for you.

How to format a number with thousands separators

Short answer

number_format() takes the number, the number of decimal places, the decimal separator and the thousands separator:

<?php
 
echo number_format(1234567.891, 2, ',', ' ');

Output:

1 234 567,89

Without the fraction

With zero decimal places the number is rounded to a whole:

<?php
 
echo number_format(1234567.891, 0, '', ' ');

Output:

1 234 568

The English format

By default thousands are separated by a comma and the fraction by a dot:

<?php
 
echo number_format(1234567.891, 2);

Output:

1,234,567.89

Functions used in this recipe

  • round — Rounds a number to a given number of digits.
  • sprintf — Returns a string built from a format template.

Thanks for reading!

Donate to the site — we work hard for you.