问题与浮游物! (在PHP中) [英] Problem with Floats! (in PHP)

查看:110
本文介绍了问题与浮游物! (在PHP中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  $ v = 5.2 * 3; 

if($ v == 15.6){
echo'Everything is fine =)';
} else {
echo'好吧,有什么地方是错的:S';
var_dump($ v);






  $ v = 5.2 * 3; 

if($ v> 15.6 AND $ v <15.60000000001){
echo'我们注定:S';
var_dump($ v); // float 15.6
} else {
echo'Everything is fine =)';
}

我想这与内部表示或一些模糊的低级别有关东东?如果是这样的话,我该如何解决这个问题?
在使用这个来计算真实货币交易之前还有什么我应该知道的?

解决方案

我确定这是一个重复,所以我做这个CW

  $ v = 5.2 * 3; 
if(bccomp($ v,15.6)=== 0){
echo'Everything is fine =)';
} else {
echo'好吧,有什么地方是错的:S';
var_dump($ v); // float 15.6

code $


将给出'Everything is fine =''


What's wrong with php floats?

$v = 5.2 * 3;

if($v==15.6) {
    echo 'Everything is fine =)';
} else {
    echo 'Okay something is wrong :S';
    var_dump($v); // float 15.6
}

Check this out too:

$v = 5.2 * 3;

if($v>15.6 AND $v<15.60000000001) {
    echo 'We are doomed :S';
    var_dump($v); // float 15.6
} else {
    echo 'Everything is fine =)';
}

I guess it has something to do with the internal representation or some obscure low level stuff? If that's the case, how do I get around this issue? Anything else I should know before using this to calculate real money transactions?

解决方案

I am sure this is a duplicate, so I'm making this a CW

$v = 5.2 * 3;
if (bccomp($v, 15.6) === 0) {
    echo 'Everything is fine =)';
} else {
    echo 'Okay something is wrong :S';
    var_dump($v); // float 15.6
}

will give 'Everything is fine =)'

这篇关于问题与浮游物! (在PHP中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆