www.webdeveloper.com
Results 1 to 9 of 9

Thread: Decimal number is recognized as whole number

  1. #1
    Join Date
    Jul 2009
    Posts
    72

    Decimal number is recognized as whole number

    Hello

    I have a small problem. I have an input box on one page that posts a number into an int table/row in a mysql database. But if i for instance enter 0,50 the number becomes 0 in the database.

    I have tried with (int) before the variable, or intval/floatval etc, but i cannot seem to get the number correct. Please help

  2. #2
    Join Date
    Mar 2010
    Posts
    2,803
    My recommendation would be to do form data validation to make sure you have clean data before inserting or updating your database.

    In reality, data validation should be done on at least the server side because client side validation can be bypassed by mischievous users.

    For 0,50 what is the desired value you would want that to equate to.?

  3. #3
    Join Date
    Jul 2009
    Posts
    72
    i want the exact number, if i enter 0,50 i want it to input 0,50

    It seems it may be some conflict with the datatypes from the $_POST array and the INT datatype in the database maybe? If so, how can i make a string value to an exact decimal number?

  4. #4
    Join Date
    Mar 2010
    Posts
    2,803
    If using a MySQL database then the data type for the table column needs to be decimal or numeric - they're both the same.

    As part of your validation, make sure 0,5 is either rejected or changed to 0.5

  5. #5
    Join Date
    Jul 2009
    Posts
    72
    So should this be correct and working:

    Code:
    $amount = (int) str_replace(',','.', $data['amount']);

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,338
    The key here is that if the MySQL column is defined as a float or decimal type, you must use the "." (period/full stop) character as the decimal separator, not the "," (comma) in your SQL. If you want to allow the user to input numbers using the comma, then you will need to convert it to a period before using it in your query.
    "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

  7. #7
    Join Date
    Jul 2009
    Posts
    72
    I solved it, I changed the mySQL datatype to float and changed my code to
    Code:
    $amount = floatval(str_replace(',','.', $data['amount']));

  8. #8
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,338
    Quote Originally Posted by Reis View Post
    So should this be correct and working:

    Code:
    $amount = (int) str_replace(',','.', $data['amount']);
    In cases where the user enters something like "1234,56" that should be fine. However, what if the user enters "1,234.56" or "12,345,678" or "12.345,6". (Thus the problem with different standards around the world. )
    "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

  9. #9
    Join Date
    Jul 2009
    Posts
    72
    I understand, but its not a problem, this is for an application only used by me and my 2 work-mates

Thread Information

Users Browsing this Thread

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

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