In php, the division method (round, ceil, floor) and ceilfloor

Source: Internet
Author: User
Tags rounds

In php, the division method (round, ceil, floor) and ceilfloor

In PHP, you need to use the following method to get the result of division:

1. round: Rounding

The round () function rounds a floating point number.

Syntax: round (x, prec)

Parameters Description
X Optional. Specifies the number to be rounded.
Prec Optional. Specifies the number of digits after the decimal point.


Returns the result of rounding x to the specified precision prec (number of digits after decimal point. Prec can also be a negative number or zero (default ).

Tip: by default, PHP cannot correctly process strings similar to "12,300.2.

Example:

1 <?php2 echo(round(0.60));3 echo(round(0.50));4 echo(round(0.49));5 echo(round(-4.40));6 echo(round(-4.60));7 ?>

Output:

1 12 13 04 -45 -5

 

2. ceil: rounded up

The ceil () function rounds up to the nearest integer.

Syntax: ceil (x)

Parameters Description
X Required. Specifies the number to be rounded.


Description: return the next integer not less than x. If x has a decimal part, the return value is one digit. The type returned by ceil () is still float, because the float value range is usually larger than integer.

Example:

1 <?php2 echo(ceil(0.60);3 echo(ceil(0.40);4 echo(ceil(5);5 echo(ceil(5.1);6 echo(ceil(-5.1);7 echo(ceil(-5.9));8 ?>

Output:

1 12 13 54 65 -56 -5

 

3. floor: round down

The floor () function rounds down to the nearest integer.

Syntax: floor (x)

Parameters Description
X Required. Specifies the number to be rounded.


Note: the return value is the next integer not greater than x, and the decimal part of x is rounded out. The return type of floor () is still float, because the float value range is usually larger than that of integer.

Example:

1 <?php2 echo(floor(0.60));3 echo(floor(0.40));4 echo(floor(5));5 echo(floor(5.1));6 echo(floor(-5.1));7 echo(floor(-5.9))8 ?>

Output:

1 02 03 54 55 -66 -6

 

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.