Php integer acquisition method and instance summary
- $ A = 20;
- $ B = 6;
- Echo ($ a/$ B )."
"; // Out 3.3333333333333
- Echo ceil ($ a/$ B )."
"; // Out 4
- Echo floor ($ a/$ B )."
"; // Out 3
- Echo round ($ a/$ B )."
"; // Out 3
- // By bbs.it-home.org
- ?>
In addition, we will introduce four common methods for php to take integers. It mainly uses four functions: ceil, floor, round, and intval. 1. the ceil-first method returns an integer not less than the value if float ceil (float value) is an integer. The type returned by ceil () is still float, because the float value range is usually larger than integer. Example:
- Echo ceil (4.3); // 5
- Echo ceil (9.999); // 10
- ?>
2. the floor-rounding method returns the next integer not greater than the value of float floor (float value) and rounds the fractional part of the value. The return type of floor () is still float, because the float value range is usually larger than that of integer. Example:
- Echo floor (4.3); // 4
- Echo floor (9.999); // 9
- ?>
3. round-rounding a floating point number indicates float round (float val [, int precision]) returns the result of rounding val according to the specified precision (number of digits after decimal point. Precision can also be negative or zero (default ). Example:
- 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
- ?>
4. intval-an example of converting a variable to an integer type:
- Echo intval (4.3); // 4
- Echo intval (4.6); // 4
- ?>
|