PHP number_format function Output number format, add thousands of decimal symbols

Source: Internet
Author: User
Tags float number

When the output data is displayed on the screen, if the data is large, more digits, it will look more laborious, there is a more intuitive way is to use the thousand, that is, every three digits display a comma, so you can quickly know the size of the number, not a bit to slowly count.

Happily, there are special functions in PHP that can do this task, and can automatically add thousands of points when outputting data.

String Number_format (float number [, int decimals [, String Dec_point, String thousands_sep]])

Number_format has four parameters, the first parameter is the number to output (floating-point type), this parameter is required, the following three parameters are optional, where two of the following parameters are either none or all

Number required. 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".

String Number_format (
float number,//numbers to output
int decimals,//decimal digits, defaults to 0
String dec_point,//decimal point representation, defaults to.
String THOUSANDS_SEP//thousand-digit representations, implied,
)

Let's try an example.

echo Number_format (' 1234.56 ');
echo Number_format (' 1234.56 ', 1);
echo Number_format (' 1234.56 ', 2);
echo Number_format (' 1234.56 ', 3);
echo Number_format (' 1234.56 ', 2, '-', '/');

The results are as follows

1,235//Rounded
1,234.6//
1,234.56//
1,234.560//small digit insufficient, add 0
The 1/234-56//decimal symbol becomes/, and the dot symbol changes to-

Example

Number_format

<?php

$number = 1234.56;

中文版 notation (default)
$english _format_number = Number_format ($number);
1,235

French notation
$nombre _format_francais = Number_format ($number, 2, ', ', ');
1 234,56

$number = 1234.5678;

中文版 notation without thousands seperator
$english _format_number = Number_format ($number, 2, '. ', ');
1234.57

?>

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.