Results 1 to 3 of 3

Thread: Unwanted number rounding when inserting into database

  1. #1
    Join Date
    Jul 2008

    Unwanted number rounding when inserting into database

    Hi everyone,

    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.

    Thank you

  2. #2
    Join Date
    Aug 2004
    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.
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  3. #3
    Join Date
    Apr 2008
    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).

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center