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);

 

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.