Home Backend Development PHP Tutorial PHP floor function problem.

PHP floor function problem.

Aug 08, 2016 am 09:31 AM
floor Money pow quot

Copy after login

I am currently writing a project about finance. There are a lot of decimal issues involved. Once when I used floor to round down to two decimal places, I suddenly found that the following code was abnormal.

$money=271.28;
echo $money=floor($money*pow(10,2))/100
Copy after login
I was surprised to find that the output was not 271.28 but 271.27!

Later I checked a lot of information and saw this on the php official website:

The precision of floating point numbers is limited, can be accurately expressed in decimal Rational numbers such as 0.1 or 0.7, no matter how many mantissas there are, cannot be accurately represented by the binary used internally, and therefore cannot be converted to binary format without losing a little bit of precision. This can lead to confusing results: for example, floor((0.1+0.7)*10) will usually return 7 instead of the expected 8, because the internal representation of the result is actually It is similar to 7.99999999999999991118.... So never believe that the floating point number result is accurate to the last digit, and never compare whether two floating point numbers are equal.

Later I printed the result of the above operation:

printf("%.12f", $money*pow(10,2));
Copy after login
found that the result was: 27127.999999999996

It turned out to be an accuracy problem. No unified solution has been found yet. I can only add

if ($money - round ( $money, 2 ) < 0.00001) {

}else{
$money=floor($money*pow(10,2))/100;
}
Copy after login
to the code logic to avoid reprocessing the data that has been accurate to two digits.

If anyone knows another way to implement the above function (accurate to two decimal places and discard whatever the third digit is), please let me know.

The above introduces the problem of PHP's floor function. , including relevant content, I hope it will be helpful to friends who are interested in PHP tutorials.

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

How to use MySQL's FLOOR function to round down How to use MySQL's FLOOR function to round down Jul 25, 2023 pm 12:45 PM

How to use MySQL's FLOOR function to round down In MySQL, the FLOOR function is used to round down. The FLOOR function is a very useful tool if you need to round down a floating point number or a number with a decimal point to the nearest integer. This article will introduce how to use MySQL's FLOOR function and provide some practical example code. First, let's understand the syntax of the FLOOR function. The syntax of the FLOOR function is as follows: FLOOR(x) where x represents

Calculate the power of a numerical value using Java's Math.pow() function Calculate the power of a numerical value using Java's Math.pow() function Jul 25, 2023 pm 06:25 PM

Use Java's Math.pow() function to calculate the power of a numerical value. In mathematical operations, we often need to calculate the power of a numerical value. In Java, we can use the Math.pow() function to perform exponentiation operations. This article will introduce how to use the Math.pow() function and give some code examples. The Math.pow() function is a static method in the mathematics library in Java, used to calculate the power of a value and return the calculation result. Its syntax is as follows: Math.p

What do PoS and PoW mean? What's the difference? Which one is better? What do PoS and PoW mean? What's the difference? Which one is better? Dec 16, 2024 pm 06:50 PM

This article takes an in-depth look at two mainstream consensus mechanisms: Proof of Stake (PoS) and Proof of Work (PoW). PoS requires validators to pledge tokens to obtain the power to verify transactions. It has the advantages of energy saving and good scalability, but it may also lead to concentration of wealth. PoW requires miners to solve complex mathematical problems to verify transactions. It is more secure and decentralized, but it consumes a lot of energy and has poor scalability. This article compares the principles, advantages and disadvantages, and applications of PoS and PoW in the blockchain field to help readers understand the key roles they play in the consensus mechanism.

不用数据库来实现用户的简单的下载,代码如下,但是却不能下载,请高手找下原因,文件路劲什么的没有关问题 不用数据库来实现用户的简单的下载,代码如下,但是却不能下载,请高手找下原因,文件路劲什么的没有关问题 Jun 13, 2016 am 10:15 AM

不用数据库来实现用户的简单的下载,代码如下,但是却不能下载,请高手找下原因,文件路劲什么的没问题。

图片消失怎么解决 图片消失怎么解决 Apr 07, 2024 pm 03:02 PM

图片消失如何解决先是图片文件上传$file=$_FILES['userfile'];  if(is_uploaded_file($file['tmp_name'])){$query=mysql_query("INSERT INTO gdb_banner(image_src ) VALUES ('images/{$file['name'

图片消失怎么解决 图片消失怎么解决 Jun 13, 2016 am 10:09 AM

图片消失如何解决先是图片文件上传$file=$_FILES['userfile'];  if(is_uploaded_file($file['tmp_name'])){$query=mysql_query("INSERT INTO gdb_banner(image_src ) VALUES ('images/{$file['name'

为什么小弟我在php上写的这个代码,在浏览器上什么都不显示 为什么小弟我在php上写的这个代码,在浏览器上什么都不显示 Jun 13, 2016 am 10:24 AM

为什么我在php上写的这个代码,在浏览器上什么都不显示啊

See all articles