How to convert 2.131 into an integer in php

藏色散人
Release: 2023-03-13 21:36:01
Original
1893 people have browsed it

php method to convert 2.131 into an integer: 1. Use the floor function for rounding; 2. Use the ceil function to achieve rounding; 3. Use the round function for rounding.

How to convert 2.131 into an integer in php

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

How does php convert 2.131 into an integer?

3 ways to convert decimals into integers in PHP:

This article mainly introduces 3 ways to convert decimals into integers in PHP. In fact, it is built-in with PHP. There are 3 functions, namely floor, ceil and round. Friends who need them can refer to the following

float floor (float value) rounding by rounding

Return the next integer not greater than value, and round off 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.

The code is as follows:

echo floor(4.3);   // 4
echo floor(9.999); // 9
echo floor(2.131); // 2
Copy after login

float ceil (float value) Rounding to an integer

Returns the next integer that is not less than value, value if If there is a decimal part, round it up to one digit. The type returned by ceil() is still float because the range of float values ​​is usually larger than that of integer.

The code is as follows:

echo ceil(4.3);    // 5
echo ceil(9.999);  // 10
echo ceil(2.131);  //3
Copy after login

float round (float val [, int precision]) Round floating point numbers

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

The code is as follows:

echo round(2.131);         // 2
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.04
echo round(5.055, 2);    // 5.06
Copy after login

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to convert 2.131 into an integer in php. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!