www.webdeveloper.com
Results 1 to 3 of 3

Thread: Unwanted number rounding when inserting into database

  1. #1
    Join Date
    Jul 2008
    Location
    Bristol
    Posts
    77

    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
    Michael

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,529
    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

    eBookworm.us

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



Recent Articles