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

Jul 03, 2018 pm 03:49 PM
ceil floor intval round

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What does round mean in php What does round mean in php Mar 10, 2023 am 10:04 AM

In PHP, round means "rounding" and is a built-in function that converts floating point numbers into integers. This function can round floating point numbers and return an integer value of type float. The syntax is "round(number, precision,mode);".

How to divide and round using PHP's round() function How to divide and round using PHP's round() function Mar 21, 2023 pm 04:32 PM

The round() function is a very useful function in the PHP number formatting library, which can round floating point numbers to a specified number of decimal places. However, since PHP's division operation may suffer from infinite decimals or loss of precision, rounding of the divisor is also necessary. Next, we will explain in detail how to use PHP's round() function to divide and round.

How to use ROUND function to intercept decimal places in MySQL How to use ROUND function to intercept decimal places in MySQL Jul 13, 2023 pm 09:21 PM

How to use the ROUND function in MySQL to intercept the number of decimal places. In MySQL, you can use the ROUND function to intercept the number of decimal places. The ROUND function rounds a number to a specified number of decimal places. The following will introduce you to the use of ROUND function in detail and provide code examples. Syntax: ROUND(X,D)X represents the number to be rounded, and D represents the number of decimal places to be retained. Example of using the ROUND function to intercept the number of decimal places: Suppose there is a table named produc

How to use MySQL's FLOOR function to round down How to use MySQL's FLOOR function to round down Jul 25, 2023 pm 12:45 PM

How to use MySQL's FLOOR function to round down In MySQL, the FLOOR function is used to round down. The FLOOR function is a very useful tool if you need to round down a floating point number or a number with a decimal point to the nearest integer. This article will introduce how to use MySQL's FLOOR function and provide some practical example code. First, let's understand the syntax of the FLOOR function. The syntax of the FLOOR function is as follows: FLOOR(x) where x represents

How to use the ceil function to round up floating point numbers in PHP How to use the ceil function to round up floating point numbers in PHP Jun 26, 2023 pm 02:21 PM

In PHP, the ceil function is used to round up floating point numbers. This function can very conveniently handle operations on some specific floating point numbers, while also ensuring the accuracy of the data. In this article, we will introduce in detail the method and application scenarios of how to use the ceil function to round up floating point numbers in PHP. 1. Syntax and parameters of ceil function The basic syntax of ceil function is very simple, as shown below: ceil(float$number) This function only accepts one parameter, that is

PHP practical skills: master the correct usage of intval function PHP practical skills: master the correct usage of intval function Mar 09, 2024 pm 09:27 PM

The intval function in PHP is a function used to convert a variable to an integer type. Its usage is relatively simple, but there are some skills and precautions that need to be mastered. Correct use of the intval function can effectively handle data type conversion issues and avoid errors in the program. The basic usage of the intval function The basic syntax of the intval function is as follows: intval($var,$base=10) where $var represents the variable to be converted to an integer,

Write a one-line C function to round floating point numbers Write a one-line C function to round floating point numbers Aug 26, 2023 pm 01:53 PM

Here we will see how to write a one line C function that can round floating point numbers. In order to solve this problem, we have to follow the following steps. Get a number If the number is positive, add 0.5 otherwise, subtract 0.5 Use type conversion to convert the floating point value to an integer Example #include<stdio.h> intmy_round(floatnumber){ return(int)(number<0?number- 0.5:number+0.5);}intmain(){&nbsp

PHP Expertise: Exploring the underlying mechanics of the intval function PHP Expertise: Exploring the underlying mechanics of the intval function Mar 09, 2024 pm 02:15 PM

PHP is a popular server-side scripting language that is widely used in website development and application development. In PHP, there are many built-in functions that can help developers simplify the programming process. One of the commonly used functions is the intval function. In this article, we will deeply explore the underlying mechanism of the intval function and use specific code examples to help readers better understand its functions and effects. First, let us understand the basic usage of the intval function. intval function is used

See all articles