I have a function that receives a value amount
, that is a float number. This number is multiplied by 100 to be converted to a whole number, before it is saved into the database. Dumping the variable after the multiplication shows that its value is 6699. When saving the number to the db (in an int column) it is saved as 6688 though! What can be wrong
Asked
Active
Viewed 40 times
1

mrateb
- 2,317
- 5
- 27
- 56
-
show the code to make it more clear – Pratik Karmakar Oct 25 '17 at 12:03
-
1try to round the amount `round($amount)` – madalinivascu Oct 25 '17 at 12:04
-
1'amount' => '66,99' , this value is not integer – Pratik Karmakar Oct 25 '17 at 12:07
-
Yes I mutlitply by 100 before saving. I dumped the value just before $model->save() and the value just before $model->save() is 6699 – mrateb Oct 25 '17 at 12:10
-
@madalinivascu what do you mean? :) – mrateb Oct 25 '17 at 12:17
-
1php saves in memory 6699 as 6698.99999999999 so when you send it to the db its truncated to 6698 – madalinivascu Oct 25 '17 at 12:19
-
Actually I think this is the issue. I dumped the variable and it turns out that its float. Ill try changing it to int – mrateb Oct 25 '17 at 12:22
-
see https://stackoverflow.com/questions/588004/is-floating-point-math-broken for more info – madalinivascu Oct 25 '17 at 12:22
-
Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/157467/discussion-between-mrateb-and-madalinivascu). – mrateb Oct 25 '17 at 12:26