Method of division Rounding in PHP (Round,ceil,floor)

Source: Internet
Author: User
Tags rounds

When you encounter a situation in PHP that requires the results of division to be rounded, you need to use the following methods:

1. Round: Rounding

The round () function rounds a floating-point number.

Syntax: Round (x, PREC)

Parameters Describe
X Optional. Specifies the number to round.
Prec Optional. Specifies the number of digits after the decimal point.


Description: Returns the result of rounding x according to the specified precision Prec (number of digits after decimal point). Prec can also be negative or 0 (the default value).

Tip: PHP does not correctly handle strings like "12,300.2" by default.

Cases:

 1 <? Php2 echo (round (0.60< Span style= "color: #000000")); 3 echo (round (0.504 echo (round (0.495 echo (round ( -4.406 echo (round ( -4.607,             

Output:

1 12 13 04-45-5   

2. Ceil: Rounding up

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

Syntax: Ceil (x)

Parameters Describe
X Have to. Specifies the number to round.


Note: Returns the next integer that is not less than x, or x if there are fractional parts. The type returned by Ceil () is still float, because the range of float values is usually larger than the integer.

Cases:

1 <?Php2echo (ceil (0.603 echo (ceil (0.40); 4 echo (ceil (55 echo (ceil (5.16 echo (ceil ( -5.17 echo (ceil ( -5.98,             

Output:

1 12 13 54 65-56-5    

3. Floor: Rounding Down

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

Syntax: Floor (x)

Parameters Describe
X Have to. Specifies the number to round.


Description: Returns the next integer not less than x, and the fractional part of X is rounded off. The type returned by floor () is still float, because the range of float values is usually larger than the integer.

Cases:

1 <?Php2echo (floor (0.603 echo (floor (0.404 echo (floor (55 echo (floor (5.16 echo (floor ( -5.17 echo (floor ( -5.98?         

Output:

1 02 03 54 55-66-6    

Method of division Rounding in PHP (Round,ceil,floor)

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.