PHP number format Function | How to format number in php

PHP number formatPHP number_format():- PHP number format function gives you a simple way to format numbers for displaying to the user. it is an inbuilt function that is used used to formate a number with grouped thousands. you can separate thousands with commas or other separators, choose the number of decimal places and choose the decimal point character to display (or display no decimals).
The PHP number format returns the formatted number on success otherwise it offers E_WARNING on failure when displaying a number in web pages, it’s nice to format the number in an easy-to-read way.

Syntax: number_format($number, $decimals, $decimalpoint, $separator)

Parameter Values

Parameter Description
$number This parameter is required. that specify the number to be formatted. If no alternative or other parameters are set, then the number goes to be formatted without the decimals & with a comma (,) as the thousands separator.
$decimals It is an optional parameter, Specifies how many decimals. If this parameter is set, then the number will be formatted with a dot (.) as a decimal point.
$decimalpoint Optional, used to specify the string to use for the mathematical notation or decimal point.
$separator A nonmandatory parameter used to specify what string to use for the thousands separator. the first character of a separator is used. If this parameter is given, then all different or other parameters are required. as an example, “aaa” can offer identical output as “a”

Return Value: In success condition, It returns Formatted Number otherwise it gives E_WARNING in failure.

PHP Number Formate:- Basic number formatting

The basic range typeat|data formatting|information|info} form number_format() accepts numbers to formate, and returns a string containing the formatted range, rounded to the closest integer, with commas between every cluster of thousands:

Important Note:
PHP number_format()function performs that it returns a string containing separator characters, therefore, we can’t use the output of PHP number format as a numeric value with different functions or maths operators.
Therefore, you do all of your calculations on the original numeric value, and also convert the value
to a string with PHP number format function, once you’re able to show it to the user.

PHP Number Formate:- Using different separators

By default, number_format() uses a comma for the thousands apparatus and a dot for the mathematical

notation. therefore, to use completely different separators, pass the new mathematical notation or decimal

point separator as the third argument, and also the new thousands separator as the fourth argument.
The given below the following example shows a number using the French convention — a space for

the thousands separator and a comma for the decimal point.

furthermore, If you wanted to displayed a number in English notation rounded to 2 decimal places, but without thousands of separators, then you can write the following program:
Important Note: PHP’s localeconv() function:- you can use this function to find out the computer’s current locale settings for the decimal point and thousands separator — handy for plugging into PHP number_format().

PHP Number Format Function:- Understand More Example:



Program Condition: Therefore, if you want to return a price: One parameter will round the number (formatted without decimals). Two (2) parameters should give us the result which you want, let’s see the program: