Home >Backend Development >PHP Tutorial >Summary of PHP floating point number usage and problems_PHP tutorial
There are many floating-point number processing functions in PHP, including ceil, floor, round, and intval. After processing by them, we can return the decimal places we want for the floating-point number. Let me introduce some usage of floating-point numbers in PHP. and problem analysis.
1. How to use PHP floating point numbers
PHP floating point type rounding ceil — further rounding
Description
float ceil (float value)
Returns the next integer that is not less than value. If value has a decimal part, it is rounded up. The type returned by ceil() is still float because the range of float values is usually larger than that of integer.
Example 1. ceil() example
The code is as follows | Copy code | ||||
echo ceil(4.3); // 5 echo ceil(9.999); // 10
?> |
Description
float floor (float value)代码如下 | 复制代码 |
< ?php |
Example 1. floor() example
The code is as follows | Copy code | ||||
< ?php echo floor(4.3); // 4 echo floor(9.999); // 9 ?>
|
PHP floating point type rounding — rounding floating point numbers
Description
代码如下 | 复制代码 |
< ?php echo intval(4.3); //4 echo intval(4.6); // 4 ?> |
Example 1. round() example
The code is as follows | Copy code | ||||||||||||||||||||||||||||||||||||||||||||
< ?php echo round(3.4); // 3
In other words, it is not possible to use the most convenient method of PHP to convert floating point numbers into strings or display them. You must use printf/sprintf to convert floating point numbers into strings.
However, I missed one thing, which is the answer to this common question:
Why is the output 57? Is it a PHP bug? I believe that many students have had such questions, because there are many people asking me similar questions, not to mention that people often ask on bugs.php.net... To understand this reason, we first need to know the representation of floating point numbers (IEEE 754): Floating point numbers, taking 64-bit length (double precision) as an example, will be represented by 1 sign bit (E), 11 exponent bits (Q), and 52-bit mantissa (M) (a total of 64 bits). Sign bit: The highest bit represents the sign of the data, 0 represents a positive number, and 1 represents a negative number. Exponent bit: indicates the data raised to the power of base 2, and the exponent is represented by an offset code Mantissa: Indicates the significant digits after the decimal point of the data.
0.58 -> 0.57999999999999996
As for the specific floating point multiplication of 0.58 * 100, we will not consider it in detail. Those who are interested can look at the floating point. We will look at it vaguely through mental arithmetic... 0.58 * 100 = 57.999999999 Then if you intval it, it will naturally be 57… It can be seen that the key point of this problem is: "Your seemingly finite decimal is actually infinite in the binary representation of the computer"
When I was developing a contract management system recently, it involved comparing two floating point numbers, which made me very depressed. A long time ago, I didn’t know where I heard the “truth” of “Don’t use the equal sign to compare floating point numbers”. I used it regularly, and it seemed that there was no problem. But this time the problem finally came.
I later learned that in PHP, to compare the sizes of two floating point numbers, you can use bccomp (parameter 1, parameter 2, decimal places) to compare.
float(12300) true http: //www.bkjia.com/PHPjc/632704.htmlTechArticleThere are many floating point number processing functions in php, including ceil, floor, round, intval, which are processed by them After that, we can return the decimal places we want for the floating point number. Let me introduce one...
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 Previous article:php time() uses the date function and the local computer and server time are different_PHP tutorialNext article:php time() uses the date function and the local computer and server time are different_PHP tutorial Related articlesSee more |