简体   繁体   中英

PHP: Weird typecasting and rounding

I have some code, which checks if a paid amount on our account is the same as it should be based on the bought items.

Here is some "Pseudocode":

$income = $this->getIncome();
$invoiceSum = $order->getInvoiceSum();
if($income != $invoiceSum ) echo "Error";

which gives me the "Error" output, even if the values are the same.

The value of $income is from a Rest API (JSON), where the $invoiceSum is calculated by my code based on the net values all ordered products multiplied by the tax rate.

If i do:

echo $income . " " . gettype($income) . "<br>";
echo $invoiceSum . " " . gettype($invoiceSum) . "<br>";

I get:

19.71 double
19.71 double

Which is what I expect.

So I tried to calculate as intvalues (=Cent instead of €). And now the weird part starts:

echo intval($income * 100);
echo intval($invoiceSum * 100);

and i get:

1970
1971

Hm? How to fix this, so i get the correct value?

From here , to get the output you want, you might have to do this

var_dump( (int)($income * 100) );
var_dump( (int)($invoiceSum * 100) );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM