Home > Backend Development > PHP Tutorial > php的floor函数问题

php的floor函数问题

WBOY
Release: 2016-06-23 13:41:33
Original
1147 people have browsed it

$money=271.29;
echo $money=floor($money*pow(10,2));
这个打印是27129

$money=271.28;
echo $money=floor($money*pow(10,2));
而打印的结果却是27127


回复讨论(解决方案)

$money=271.28;printf("%.12f", $money*pow(10,2)); //27127.999999999996echo $money=floor($money*pow(10,2)); //27127
Copy after login
Copy after login

27127.999999999996 截尾后不就是 27127 吗?

$money=271.28;printf("%.12f", $money*pow(10,2)); //27127.999999999996echo $money=floor($money*pow(10,2)); //27127
Copy after login
Copy after login

27127.999999999996 截尾后不就是 27127 吗?


为什么不是27128.00000000而是27127.999999999996

因为是浮点数,自然就存在精度问题
请补习一下有关浮点数的相关知识

?於php 浮??
可以看看??: http://www.laruence.com/2013/03/26/2884.html

http://php.net/manual/zh/function.floor.php
http://php.net/manual/zh/function.ceil.php
http://php.net/manual/zh/function.round.php

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