Home  >  Article  >  Backend Development  >  How to convert 2.131 into an integer in php

How to convert 2.131 into an integer in php

藏色散人
藏色散人Original
2021-11-18 09:41:412024browse

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

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

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

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!

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