Summary of php rounding methods

Source: Internet
Author: User
Sum up the php rounding method: (1) rounded up: ceil () & mdash; gets the next integer not smaller than the parameter. if there is a decimal part, it goes into one, the returned type is float1 & lt ;? Php2 // Get 3 $ c_1ceil (3.4); 4 echo $ c_1. & amp; #39; & lt; br/& gt; & amp; #39; 56 $ c_2

Summary of php rounding methods:

(1) rounded up: ceil () -- gets the next integer not smaller than the parameter. if there is a decimal part, the return type is float.

1
2
// Obtain the largest value
3
$ C_1 = ceil (3.4 );
4
Echo $ c_1 .'
';
5
 
6
$ C_2 = ceil (3.5 );
7
Echo $ c_2 .'
';
8
?>
Result:

 

 

 

(2) rounded down: floor () -- gets the next integer not greater than the parameter. if there is a decimal part, the return type is float.

1
2
// Small
3
$ F_1 = floor (3.4 );
4
Echo $ f_1 .'
';
5
 
6
$ F_2 = floor (3.5 );
7
Echo $ f_2 .'
';
8
?>
Result:

 

 

 

(3) rounding method: round () -- rounds the floating point number. the function has two parameters. The second parameter can be used to set the decimal places reserved for the result.

01
02
// Rounding method
03
$ R_1 = round (3.4 );
04
$ R_2 = round (3.5 );
05
Echo $ r_1 .'
';
06
Echo $ r_2 .'
';
07
 
08
$ R_3 = round (3.444, 2 );
09
$ R_4 = round (3.445, 2 );
10
Echo $ r_3 .'
';
11
Echo $ r_4 .'
';
12
?>
Result:

 

 

 

 

(4) forced conversion method: intval () -- convert a variable to an integer type. a function has two parameters. The first parameter can be an array or any type variable other than a class, the second parameter sets the hexadecimal value of the first parameter, and then converts the number of hexadecimal values to a decimal integer.

 
01
02
// Forced conversion
03
$ I _1 = intval (3.4 );
04
Echo $ I _1 .'
';
05
 
06www.2cto.com
$ I _2 = intval (3.5 );
07
Echo $ I _2 .'
';
08
 
09
$ I _3 = intval (a, 16 );
10
Echo $ I _3 .'
';
11
?>


Author: frylan

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.