PHP functions for formatting numbers by thousands Number_format ()

Source: Internet
Author: User


Format numbers:

<?phpecho Number_format ("1000000"). " <br> "; Echo Number_format (" 1000000 ", 2)." <br> "; Echo Number_format (" 1000000 ", 2,", ",". ");? >

Definition and usage

The Number_format () function formats numbers by using thousands of groupings.

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


Number_format (Number,decimals,decimalpoint,separator)

Parameter description

Number required. The number to format. If no other parameters are set, the number is formatted with no decimal point and comma (,) as the thousand separator.

Decimals is optional. How many decimal places are specified. If this parameter is set, the number is formatted with a dot (.) as the decimal point.

Decimalpoint is optional. Specifies a string to use as a decimal point.

Separator is optional. Specifies the string to use as the thousand separator. Only the first character of the parameter is used. For example, "XXX" only Output "X".
Note: If this parameter is set, all other parameters are required.

Technical details

Return value: Returns the formatted number.

PHP version: 4+

Update log: Since PHP 5.4, this function supports multibyte in Parameters Decimalpoint and separator. In previous versions, the value used the first byte of each delimiter.

More examples

Example 1

You want to return a price: one parameter will round the number (formatted with no decimal digits), and two parameters will give you the desired result:

<?php$num = 1999.9; $formattedNum = Number_format ($num). " <br> "; echo $formattedNum; $formattedNum = Number_format ($num, 2); Echo $formattedNum;? >

Interesting number_format.

Number_format (Number,decimals,decimalpoint,separator)

There are four parameters,
The first and second parameters are required, and the third and fourth are optional. But the third and fourth of the actual tests must both be present, either set or not set.
No third and fourth parameters are set:

Number_format (13526, 2); Echo 13,526.00;

If you add this number to the process, you will only get a 13!.
Set the third and fourth parameters

Number_format (23125, 2, '. ', '); Echo 23125.00;

Then the processing of the number after the operation will be executed correctly!
The third parameter of the function indicates what is represented by the ' decimal ' position, which can be defaulted. , can also be set to ', ' and other symbols. Ps: But I'm sure no one would do that.
The fourth one indicates what to use to divide the numbers every thousand. If there are no special requirements, it is best to set the operation to NULL.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.