Home  >  Article  >  Backend Development  >  Analysis of examples of floating point number operations in PHP

Analysis of examples of floating point number operations in PHP

墨辰丷
墨辰丷Original
2018-06-01 10:14:541591browse

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;

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, 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!

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