Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading a float from mySQL

I have two float variables that are equal, I retrieve them by PHP and subtract them. the result equals (7.105427357601E-15) I tried to change to double or to decimal and none of that worked. This is what I use in PHP:

$giftcard_balance = $giftcard['balance'];
$total = $product['price'];

$total -= $giftcard_balance;
echo $total;

Here instead of showing 0, it's showing (7.105427357601E-15)

Anyone can help please?

like image 227
Rahif Abboud Avatar asked Aug 13 '26 14:08

Rahif Abboud


1 Answers

When dealing with prices and account balances you will find that DECIMAL(6, 2) is much easier to deal with than FLOAT. If you experience the same problem when using DECIMAL datatype please post a specific example.

Using round() or similar is a very bad idea as it is simply masking the problem, not fixing it.

like image 129
nnichols Avatar answered Aug 16 '26 17:08

nnichols