PHP Number_format () function definition and usage _php tips

Source: Internet
Author: User
The Number_format () function formats a number by a thousands group.

Grammar
Number_format (Number,decimals,decimalpoint,separator)
Parameters Description
Number

Necessary. The number to format.

If no other parameters are set, the number is formatted with no decimal point and comma (,) as the separator.

Decimals Optional. Specify how many decimal places. If this argument is set, the number (.) is used as the decimal point to format the digits.
Decimalpoint Optional. A string that is used as a decimal point.
Separator

Optional. A string that is used as a thousand separator.

Use only the first character of the parameter. For example, "XYZ" outputs only "X".

Note: If this parameter is set, all other parameters are required.

Tips and Comments
Note: This function supports one, two, or four parameters (not three).
Example
Copy Code code as follows:

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

Output:

1,000,000
1,000,000.00
1.000.000,00

interesting number_format.
Number_format (Number,decimals,decimalpoint,separator)

has four parameters,

The first and second arguments are required, and the third and fourth are optional. But the third and fourth of the actual tests must both exist, 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 processing, 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 represents what the ' decimal point ' position is, and can be used by default. can also be set to ', ' and other symbols. Ps: But I don't think anyone would do that.
The fourth indicates what is used to divide the number at every thousand. If there is no special requirement, it is best to set the operation to NULL.

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: info-contact@alibabacloud.com 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.