Menu

error while querying float numbers!

vegetax
2005-03-08
2012-09-19
  • vegetax

    vegetax - 2005-03-08

    'select * from Item where price = 5.3'

    gives 0 results in a table with a 5.3 price value, and a float field

    'select * from item where price = 40' works fine

    Is it just me that has this problem?

     
    • Andy Dustman

      Andy Dustman - 2005-03-08

      I think it's mostly you. CAn you check and see if the mysql command-line client gives different results? I suspect floating-point approximations are the cause of your problem. For example, in the Python interpreter:

      >>> 5.3
      5.2999999999999998

      Perhaps you can't do anything about this, but since you have a price, you should probably use a DECIMAL column. MySQLdb-1.2.0 returns these as decimal.Decimal, if it is available (Python-2.4 and newer); otherwise it returns it as a float.

       
    • vegetax

      vegetax - 2005-03-09

      thanks for the tip about the Decimal! , is that the best aproach in mysqldb to work with monetary columns?

       
      • Andy Dustman

        Andy Dustman - 2005-03-09

        Well... try it and report back.

        DECIMAL SQL columns are specifically for monetary values, or values with a fixed number of fractional digits, and so is the Python Decimal type, so it only makes sense to use them together. I think it will help to avoid comparision problems.

         

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.