Why this function return -2.8421709430404E-14 ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gayanp
Forum Newbie
Posts: 1
Joined: Mon Apr 18, 2011 3:18 am

Why this function return -2.8421709430404E-14 ?

Post by gayanp »

Code: Select all

<?Php

function getDiscount() {
$discount = 100;
return (245/100) * $discount;
}

echo 245-getDiscount();

?>
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Why this function return -2.8421709430404E-14 ?

Post by Weirdan »

Because it operates on float numbers (you've got float when you divided integer by integer).
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Why this function return -2.8421709430404E-14 ?

Post by McInfo »

Try BCMath.
Post Reply