php相加float,PHP:string和float之间的总和问题

考虑这个数组

array(6) {

["id"]=>

int(346058)

["amount"]=>

string(5) "60.00"

["id_shop_where_transaction_is_done"]=>

int(300)

["id_shop_where_money_come_from"]=>

NULL

["negative_sum"]=>

float(-60)

["negative"]=>

array(11) {

[0]=>

array(5) {

["id"]=>

int(346060)

["amount"]=>

string(5) "-2.20"

["id_shop_where_transaction_is_done"]=>

int(300)

["id_shop_where_money_come_from"]=>

NULL

["id_referring_transaction"]=>

int(346058)

}

[1]=>

array(5) {

["id"]=>

int(348152)

["amount"]=>

string(5) "-7.50"

["id_shop_where_transaction_is_done"]=>

int(300)

["id_shop_where_money_come_from"]=>

NULL

["id_referring_transaction"]=>

int(346058)

}

[2]=>

array(5) {

["id"]=>

int(350163)

["amount"]=>

string(5) "-7.70"

["id_shop_where_transaction_is_done"]=>

int(300)

["id_shop_where_money_come_from"]=>

NULL

["id_referring_transaction"]=>

int(346058)

}

[3]=>

array(5) {

["id"]=>

int(351996)

["amount"]=>

string(5) "-5.20"

["id_shop_where_transaction_is_done"]=>

int(300)

["id_shop_where_money_come_from"]=>

NULL

["id_referring_transaction"]=>

int(346058)

}

[4]=>

array(5) {

["id"]=>

int(353919)

["amount"]=>

string(5) "-5.00"

["id_shop_where_transaction_is_done"]=>

int(300)

["id_shop_where_money_come_from"]=>

NULL

["id_referring_transaction"]=>

int(346058)

}

[5]=>

array(5) {

["id"]=>

int(354768)

["amount"]=>

string(5) "-2.50"

["id_shop_where_transaction_is_done"]=>

int(300)

["id_shop_where_money_come_from"]=>

NULL

["id_referring_transaction"]=>

int(346058)

}

[6]=>

array(5) {

["id"]=>

int(356650)

["amount"]=>

string(5) "-5.00"

["id_shop_where_transaction_is_done"]=>

int(300)

["id_shop_where_money_come_from"]=>

NULL

["id_referring_transaction"]=>

int(346058)

}

[7]=>

array(5) {

["id"]=>

int(361683)

["amount"]=>

string(5) "-5.00"

["id_shop_where_transaction_is_done"]=>

int(300)

["id_shop_where_money_come_from"]=>

NULL

["id_referring_transaction"]=>

int(346058)

}

[8]=>

array(5) {

["id"]=>

int(361836)

["amount"]=>

string(5) "-7.50"

["id_shop_where_transaction_is_done"]=>

int(300)

["id_shop_where_money_come_from"]=>

NULL

["id_referring_transaction"]=>

int(346058)

}

[9]=>

array(5) {

["id"]=>

int(364145)

["amount"]=>

string(5) "-7.90"

["id_shop_where_transaction_is_done"]=>

int(300)

["id_shop_where_money_come_from"]=>

NULL

["id_referring_transaction"]=>

int(346058)

}

[10]=>

array(5) {

["id"]=>

int(364426)

["amount"]=>

string(5) "-4.50"

["id_shop_where_transaction_is_done"]=>

int(300)

["id_shop_where_money_come_from"]=>

NULL

["id_referring_transaction"]=>

int(346058)

}

}

}一点解释:

$array[0]['negative_sum']是$array[0]['negative'][$i]['amount']的总和

(如果你这样做,总数是-60。)

现在,我需要检查$ array [0] ['amount']是否为negative_sum,所以有些东西。

$positive_amount = $transaction[$i]['amount'];

$negative_amount = $transaction[$i]['negative_sum'];

if ($positive_amount>abs($negative_amount)) {

$difference = -$positive_amount-$negative_amount;

var_dump($difference);

$data['difference'] = $difference;

}首先,特定情况下的if代码不会被执行,因为60不会大于abs(-60)。但它被执行。

第二个,var_dump($差异)输出是

float(-7,105427357601E-15)不明白为什么。谢谢。

Per @OptimusCrime答案。我修改了一下代码。

$positive_amount = $transaction[$i]['amount'];

$negative_amount = $transaction[$i]['negative_sum'];

var_dump(number_format($positive_amount, 90));

var_dump(number_format($negative_amount, 90));

if ($positive_amount>abs($negative_amount)) {

$difference = -$positive_amount-$negative_amount;

var_dump($difference);

$data['difference'] = $difference;

}该var_dump输出两个:

string(93) "60.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"

string(94) "-60.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值