Unwanted number rounding when inserting into database
Having a brain fart here and not sure what to do....I'm using a function in a class to insert prices into a database table, and I'm using number_format($price, 2, '.', ',') in order to make sure it's correct. However when it goes into my table, a value like 19.38 will change to 19.00 and a value like 7.07 will come out like 7.00. In other words, it's rounding the numbers and that cannot happen as these are exact prices that need to be correct. I'm using the format of DECIMAL(10,2) in my table field - I assumed this was the right one to use? If anyone can shed some light onto why it is auto rounding my numbers I would be grateful.
DECIMAL type should be fine. As to why you're losing the decimal portion, I have no real idea, unless something in your code is casting it to an integer, e.g. the wrong type in a mysqli_bind_param() or the wrong place-holder type in a sprintf() format string.
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
How to Ask Questions the Smart Way
(not affiliated with this site, but well worth reading)
I believe number_format is used for human readable numbers. If you format it like that you put string formatting into it. Databases don't read like humans and it needs only numbers. Just input the unformatted number and let mysql handle the decimal rounding (it will already).
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Tags for this Thread