Home > Article > Backend Development > Analysis of troublesome floating point operations in php
This article mainly introduces the troublesome floating-point number operations in PHP. It analyzes the use of PHP's round function for floating-point number operations in the form of examples. Friends in need can refer to it
This article analyzes the troublesome floating-point number operations in PHP with examples. floating point arithmetic. Share it with everyone for your reference, the details are as follows:
When doing e-commerce, it is inevitable to calculate the price, and then I discovered a pitfall in php. The correct value should be calculated by mouth, but the calculated value in php will be different from yours
Please Look at the code below:
$price=69.1; $count=100; $total=$price*$count-6910; echo $total;
Guess the value of the variable $total. Run this code and the output will be: -9.09494701773E-13
How to solve this problem?
Use the round function
The code is modified to:
$price=69.1; $count=100; $total=round($price*$count)-6910; echo $total;
The problem is solved, the output result is 0, correct
I hope this article will be helpful to everyone in PHP programming.
For more articles related to the analysis of troublesome floating-point operations in PHP, please pay attention to the PHP Chinese website!