PHP number format

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

PHP : number_format() function

Description

The number_format() function is used to format a number (Floating number) with grouped thousands.

Version

(PHP 4 and above)

Syntax

number_format(number, decimals, dec_point, thousands_sep) 

Parameter

Name

Description

Required /
Optional

Type

number

The input number.

Required

Float

decimals

Refer to the number of decimal points.

Optional

Integer

dec_point

Refers the separator of decimal points.

Optional

String

thousands_sep

Refers the thousands separator.

Optional

String

Note : The function accepts one, two, or four parameters (not three).

Return value

A formatted version of the number.

Value Type : String

Pictorial Presentation

php-string-number_format()

Example :

  1. $number=100000;  

  2. echo number_format($number).'
    ';  

  3. echo number_format($number, 2).'
    ';  

  4. echo number_format($number, 3).'
    ';  

  5. echo number_format($number, 2, ',', '.');  

  6. ?>  

Output :

100,000
100,000.00
100,000.000
100.000,00