# Example of how to use PHP rounding, rounding, and round functions. round_PHP tutorial

Source: Internet
Author: User
Example of PHP rounding, rounding, and using the round function. Example of using the PHP rounding, rounding, and round functions, and rounding the round decimal places: PHP keeps two decimal places and rounds them to copy the code as follows: \$ n0.1265489; example of echosprintf PHP rounding, rounding, and round function usage, rounding

Example of decimals:

PHP keeps two decimal places and rounding them

The code is as follows:

\$ N = 0.1265489;
Echo sprintf ("%. 2f", \$ n); // 0.13

We can see that the sprintf function is used to format \$ n %. 2f is the target format, where 2 represents two f represents float (floating point type) 3rd is rounded to decimal 6
Let's look at another example.

The code is as follows:

\$ N = 0.1265489
Echo substr (sprintf ("%. 3", \$ n), 0,-1); // 0.12

The code outputs a reserved 2 decimal number without rounding. In fact, we understand that the sprintf feature will be rounded to the decimal number. then we reserve one more digit and use substr to capture the first two digits.

Example of an integer:

The code is as follows:

Echo ceil (4.1); // 5
Echo ceil (9.999); // 10

The ceil function is the entire upward function. what is the upward function? That is to say, if it exceeds a little bit, it will go forward. for example, in the example, 4.1 is changed to 5.

Opposite to it, there is also a function called floor. let's look at its usage.

The code is as follows:

Echo floor (4.1); // 4
Echo floor (9.999); // 9

The features of floor are particularly obvious in the second output, that is, the number of decimal places not to be given to you, even if it is infinitely close to 10, no down integer is 9.

Round function

The code is as follows:

<? PHP
Echo round (3.4); // 3
Echo round (3.5); // 4
Echo round (3.6); // 4
Echo round (3.6, 0); // 4
Echo round (1.95583, 2); // 1.96
Echo round (1241757,-3); // 1242000
Echo round (5.045, 2); // 5.05
Echo round (5.055, 2); // 5.06
?>

The round function is described in the PHP manual as follows:

Float round (float \$ val [, int \$ precision = 0 [, int \$ mode = PHP_ROUND_HALF_UP])
Returns the result of rounding val by the specified precision (number of digits after decimal point. Precision can also be negative or zero (default ).

Round's parameter 1 is the data source, and parameter 2 is the decimal place to be retained and the last digit (for example, if you enter 2, then 3rd is the last digit) is rounded up, when it is a negative number, the length of the forward number from the last digit of the data source is 0 and the last digit is rounded to, for example, round (123456,-2) that is, 123456 is changed from 6 to 0, and the last digit 5 (the first digit is 6 and the last digit is 5) is rounded to 123500, and the output is.

Example of dropping decimal places: PHP keeps two decimal places and rounds the code as follows: \$ n = 0.1265489; echo sprintf...

Related Keywords:

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.