0

I have this update query in laravel where I subracted a value to a total_qty column with a double datatype. Example: When 250 is subtracted by 15.1 the result should be 234.9 but in the total_qty with a double datatype column, it stored 234.4. What is wrong with this?

Update Query

InventoryRecord::where('id', $dataSales[$i]['sales_inv_id'])
                ->update(['total_qty' => $dataSales[$i]['actual_inv_quantity']-$dataSales[$i]['sales_qty']]);

enter image description here

Eli
  • 1,256
  • 4
  • 29
  • 59
  • 1
    Have you verified that the operands are actually 250 and 15.1? Floating point math has a level of imprecision, but not at this order of magnitude. – El_Vanja Dec 20 '20 at 11:47
  • So what datatype should I use then if there are decimal points? – Eli Dec 20 '20 at 12:13
  • How should I perform this query to return an accurate result then? – Eli Dec 20 '20 at 12:16
  • 1
    Here's [something to read](https://stackoverflow.com/questions/33730538/difference-between-decimal-and-numeric-datatype-in-psql) regarding storage of decimals. But your problem must be elsewhere, I've never encountered floating point calculations that are .5 off. Have you dumped the variables to verify they actually contain what you think they do? – El_Vanja Dec 20 '20 at 12:18
  • Is this the only value that is off? Per @El_Vanja I have to believe you are not working with the values you think you are. – Adrian Klaver Dec 20 '20 at 17:09

0 Answers0