Home  >  Article  >  Backend Development  >  Detailed explanation of PHP rounding function code

Detailed explanation of PHP rounding function code

藏色散人
藏色散人Original
2019-04-11 10:01:443692browse

This article mainly introduces how to use PHP rounding-related functions, namely intval() function, round() function, ceil() function, floor() function. I hope it will be helpful to friends who need it. Helps!

Detailed explanation of PHP rounding function code

1. intval() function

Code example:

<?php
$num = 3.1415926;
$num2 = 3.6;
 
echo intval($num).&#39;<br>&#39;;
echo  intval($num2);
echo "<hr>";

Output:

3
3

2. round() function

Code example:

<?php
$num = 3.1415926;
$num2 = 3.6;
 
echo round($num).&#39;<br>&#39;;
echo  round($num2);
echo "<hr>";

Output:

3
4

3. ceil() function

<?php
$num = 3.1415926;
$num2 = 3.6;
 
echo ceil($num).&#39;<br>&#39;;
echo  ceil($num2);
echo "<hr>";

Output:

4
4

4. Floor() function

<?php
$num = 3.1415926;
$num2 = 3.6;
 
echo floor($num).&#39;<br>&#39;;
echo  floor($num2);

Output:

3
3

Summary of PHP rounding function:

intval() function means to obtain the integer value of a variable.

The round() function represents rounding of floating point numbers.

The ceil() function represents further rounding.

The floor() function represents rounding by rounding.

Related recommendations: "PHP Tutorial"

The above is the detailed content of Detailed explanation of PHP rounding function code. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn