I am doing simple minus from total in a loop if in a loop both variable values are same (i.e 27.99 - 27.99) I am getting "7.1054273576E-15" as a result. I don't know why? here is the code $new_price_total = 85.99; // any value from db while($new = mysql_fetch_array($get_new_total_res)){ if($new['ex_prd_price'] > 0) $new_price_total -= $new['ex_prd_price']; } $new_price_total = floatval($new_price_total) - floatval($price); // here both values are equal suppose 27.99 PHP: Any suggestion?
7.1054273576E-15 is the same as: 7.1054273576 x 10^-15 which is a pretty small number. Are you sure you want to get such number? Peace,
7.1054273576E-15 is not an error, it's a number. Specifically, it's .0000000000000071054273576. So whatever you're doing, that's the result it's getting. If you don't want scientific notation, use number_format(). If you don't have numbers in your database with such high precision, you could be seeing a floating point rounding artifact.
Your problem is that floating point arithmetic is not 'exact'.... you're seeing what happens sometimes when you do floating point arithmetic. The best thing to do is either keep all of your numbers in integers and then divide by 100, for example, or always round before you display the numbers. But in reality, its better to deal in integers whilever you can.