PHP round, rounding, round function Use example _php instance

Source: Internet
Author: User
Tags sprintf

Decimal Example:

PHP retains two decimal places and rounded up

Copy Code code as follows:

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

You can see we used the sprintf function to format the $n%.2f is the target format, where 2 indicates that two-bit f means float (floating-point) 3rd for decimal 6 is rounded
Let's look at an example.

Copy Code code as follows:

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

The code output is reserved 2 decimal is not rounded, in fact, we understand that the characteristics of the sprintf will be rounded decimal, we retain one more, and then use SUBSTR to intercept the top 2

Take the whole example:

Copy Code code as follows:

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

Ceil function is to take the whole function up, what is called up? Which means that if you go beyond a little, move on. One example of 4.1 becomes 5.

Contrary to it, there is a function called floor. Let's see how it's used.

Copy Code code as follows:

Echo Floor (4.1); 4
echo Floor (9.999); 9

The characteristic of floor is particularly noticeable in the second output, that is, not giving you as many decimal digits as infinity or nearly 10, or an integer that is not down to 9.

Round function

Copy Code code 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 according to the specified precision precision (number of digits after the decimal point). Precision can also be negative or 0 (the default value).

The parameter of the

round is a data source, parameter two is the number of decimal places to keep and then one (for example, you enter 2 then the 3rd is the next one) is rounded, when he is a negative number, from the data source the last one began to forward the corresponding length of 0 and the last rounded like round ( 123456,-2) is 123456 from 6 onwards the number two digits become 0, and the last 5 (from the first digit of the forward is 6 the last is 5) rounded, output 123500

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.