Jump to content

Why this return -2.8421709430404E-14 ?


gayanp

Recommended Posts

Thank you for your replay i cant use

 

echo 245-(int)getDiscount();

 

Discount can be change to decimal value.. i want value with 2 decimal places. please help me if you have better solution ..

 

Actually i want to get understand why this happen..

 

check this ... it's return -0.00  it cant be -0.00 i want value 0.00. please help me..

 

function getDiscount() {

$discount =0;

$discount = 100;

return (245/100) * $discount;

 

}

$y =245.00;

$x =getDiscount();

 

 

echo number_format(($y-$x),2);

 

 

 

The reason this happens is due to how a FLOAT is stored in memory. FLOAT has a loss in precision, it actually tells you how big the loss is: -2.8421709430404E-14

 

Therefor you should use BCMath, which gives you the correct result:

 

print bcsub(245, getDiscount()); // returns: 0

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.