The use of rounding round rounding, rounding up, rounding down, and fractional interception in PHP Division operations

Source: Internet
Author: User
This article mainly introduces the PHP division operations rounded rounding, up rounding, down rounding, the use of fractional interception, has a certain reference value, now share to everyone, there is a need for friends can refer to

PHP takes integer functions commonly used in four ways:
1. Direct rounding, discarding decimals, preserving integers: intval ();
2 Rounding and Rounding: round ();
3. Rounding up, there are decimals on the addition of 1:ceil ();
4. Rounding Down: Floor ().

First, intval-the variable into an integer form
Intval If the character type is automatically converted to 0, it is usually used to cast a numeric type, but it is important to note that it is recommended to use (int) if the length is too long.

Intval (5.2);  5intval (5.6);  5intval (' abc '); 0

Second, rounding: round ()
The parameter 1 is rounded according to the specified precision of parameter 2. The parameter 2 can be a negative number or 0 (the default).

Round (5.2);      5round (5.8);      6round (5.88888, 0);   6round (5.83333, 2);   5.83round (5.83353, 3);   5.834round (5201314,-2);  5201300

Three, upward rounding, there are decimals on the addition of 1:ceil ()

Echo (Ceil (0.60);   1echo (Ceil (0.40);   1echo (Ceil (5);      5echo (Ceil (5.1);    6echo (Ceil ( -5.1);   -5echo (Ceil ( -5.9));  -5

Four, downward rounding: Floor ()

echo (Floor (0.60));  0echo (Floor (0.40));     0echo (Floor (5));   5echo (Floor (5.1));  5echo (Floor (-5.1)); -6echo (Floor ( -5.9))//-6 
Related Article

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.