Analysis of examples of floating point number operations in PHP

墨辰丷
Release: 2023-03-28 21:12:07
Original
1571 people have browsed it

This article mainly introduces the analysis of examples of floating point operations in PHP, and analyzes the use of PHP's round function for floating point operations in the form of examples. Friends in need can refer to it

In e-commerce Sometimes, it is inevitable to calculate the price, and then I discovered a pitfall in PHP. If you calculate the correct value verbally, the PHP calculation will be different from yours.

Please see the following code:

$price=69.1;
$count=100;
$total=$price*$count-6910;
echo $total;
Copy after login

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;
Copy after login

The problem is solved, output the result is 0, correct

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

PHP Upload Excel file and import data to MySQL database

phpThrow Detailed explanation of exceptions and catching specific types of exceptions

##php array_merge_recursive Array merge

The above is the detailed content of Analysis of examples of floating point number operations 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!