Skip to Main Content
PHP in a Nutshell
book

PHP in a Nutshell

by Paul Hudson
October 2005
Intermediate to advanced content levelIntermediate to advanced
372 pages
11h 35m
English
O'Reilly Media, Inc.
Content preview from PHP in a Nutshell

Name

number_format()

Synopsis

    string number_format ( float num [, int decimals
    [, string decimal_point, string thousands_sep]] )

The number_format() function rounds numbers and adds commas as a thousands separator. You can pass it either one, two, or four parameters:

  • number_format($n) rounds $n to the nearest whole number and adds commas in between thousands. For example:

        $total = 12345.6789;
        echo "Total charge is \$", number_format($total), "\n";

    That will output Total charge is $12,346, because it rounds up to the nearest decimal place.

  • number_format($n,$p) rounds $n to $p decimal places, adding commas between thousands. For example:

        echo "Total charge is \$", number_format($total, 2), "\n";

    This time the output is 12,345.68, as it has been rounded to two decimal places.

  • number_format($n, $p, $t, $d) rounds $n to $p decimal places, using $t as the thousands separator and $d as the decimal separator. For example:

        echo "Total charge is ", number_format($total, 2, ".", ","), " Euros";

    The output is now 12.345,68, which swaps the period and comma, as is the norm in many European countries.

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

PHP Cookbook

PHP Cookbook

Eric A. Mann
Programming PHP

Programming PHP

Rasmus Lerdorf, Kevin Tatroe
Learning PHP

Learning PHP

David Sklar

Publisher Resources

ISBN: 0596100671Errata Page