Home > Backend Development > PHP Tutorial > PHP rounding function: introduction to the differences between ceil, floor, round and intval

PHP rounding function: introduction to the differences between ceil, floor, round and intval

不言
Release: 2023-04-02 08:58:02
Original
4172 people have browsed it

The following is a detailed introduction to the differences between the rounding functions in PHP: ceil, floor, round, intval. Friends in need can come and refer to it

We often use PHP Rounding functions, mainly: ceil, floor, round, intval.

ceil -- further rounding
Explanation
float ceil (float value)

returns no The next integer less than value. If value has a decimal part, it is rounded up by one digit. The type returned by ceil() is still float because the range of float values ​​is usually larger than that of integer.

PHP rounding function example 1. ceil() example

< ?php echo ceil(4.3); 
// 5 echo ceil(9.999); 
// 10 
?>
Copy after login

floor -- rounding by rounding
Explanation
float floor (float value)

Returns the next integer not greater than value, and rounds the decimal part of value. The type returned by floor() is still float because the range of float values ​​is usually larger than that of integer.

PHP rounding function example 1. floor() example

< ?
php echo floor(4.3); 
// 4 echo floor(9.999); 
// 9 
?>
Copy after login

round -- Round floating point numbers
Description

float round ( float val [, int precision] )
Copy after login

Returns the result of rounding val according to the specified precision (the number of decimal digits after the decimal point). precision can also be negative or zero (the default).

PHP rounding function example 1. round() example

< ?
php 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 
?>
Copy after login

intval---convert the variable into an integer type
PHP rounding function example intval()

< ?
php echo intval(4.3); 
//4 echo intval(4.6); 
//4 
?>
Copy after login

The above is the entire content of this article. I hope it will be helpful to everyone's learning. For more related content, please pay attention to PHP Chinese website!

Related recommendations:

About the definition and usage of php htmlentities() function

About using openssl in PHP7.1 Introduction to replacing mcrypt

#

The above is the detailed content of PHP rounding function: introduction to the differences between ceil, floor, round and intval. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template