Decimals

Discussion in 'Python' started by Tgone, Jun 13, 2006.

  1. Tgone

    Tgone Guest

    Hello,

    I have a price column in a MySQL table: price decimal(5,2)

    When I access them in Python with SQLObject, prices display as 15.0
    instead of 15.00. Why is this happening? I can't figure out why Python
    is trimming off the hundredth place. I'm not doing any formatting...

    Thanks,
    Tony
     
    Tgone, Jun 13, 2006
    #1
    1. Advertisements

  2. Tgone

    Laszlo Nagy Guest

    Tgone írta:
    What do you mean by 'displays as 15.0'? Do you print in with the print
    statement? If your DB access module returns decimal.Decimal instances,
    then you are right. It should print as 15.00 instead of 15.0. Try to
    determine the type of the returned value. Maybe your DB module uses
    float instead of Decimal?

    Best,

    Laszlo
     
    Laszlo Nagy, Jun 13, 2006
    #2
    1. Advertisements

  3. Tgone

    Tgone Guest

    Sorry, when I print out the variable it displays as '15.0'. The price
    is '15.00' in the database though.

    Here's my code:

    product = Product.get(2)
    print product.price # 15.0

    I'm using MySQLdb if that helps.
     
    Tgone, Jun 13, 2006
    #3
  4. Tgone enlightened us with:
    That's the same thing, isn't it? 15.0 == 15.000000000
    Try string formatting:

    print '%.2f' % product.price

    Sybren
     
    Sybren Stuvel, Jun 13, 2006
    #4
  5. Tgone

    Tgone Guest

    Yes, they're both mathematically the same. I never said they weren't...
    That works. I expected Python to display the data exactly as it is in
    the database, like most languages.
     
    Tgone, Jun 13, 2006
    #5
  6. Tgone

    Laszlo Nagy Guest

    It depends on what you get back from MySQLdb. Try this:

    import decimal
    d = decimal.Decimal("3.0000")
    print d


    Now try this:

    d = float("3.0000")
    print d


    The problem is -- probably -- with your db module. It may return a float
    instead of a Decimal. I cannot help more with MySQL, because I do not
    use it. Sorry.


    Laszlo
     
    Laszlo Nagy, Jun 13, 2006
    #6
  7. Tgone enlightened us with:
    You did act like they weren't.
    You shouldn't expect all languages and all databases to display
    float(15) the same way. They should all treat it the same in
    calculations, though.

    Sybren
     
    Sybren Stuvel, Jun 13, 2006
    #7
  8. Tgone

    Steve Holden Guest

    Well I'm sorry Python doesn't live up to your expectations, but I think
    your assertion "like most languages" is a little over-reaching.

    What's actually happening is that the database is storing the number in
    some internal format known to itself. The module you are using to access
    the database is actually converting that value to a floating-point
    number (I'm guessing here: some more modern modules will convert it to a
    decimal). That's where the scaling factor gets lost, as until fairly
    recently Python only *had* floats (well, and complex numbers)to
    represent non-integral numbers.

    So basically you are likely to be stuck with formatting the data the way
    you want to see it. This is fairly usual in my experience.

    regards
    Steve
     
    Steve Holden, Jun 13, 2006
    #8
  9. Care to give us an example of "one of those"?

    Diez
     
    Diez B. Roggisch, Jun 13, 2006
    #9
  10. Tgone

    Tim Roberts Guest

    "Like most languages?" Name one.

    C and C++ certainly will not display this with two decimal places, unless
    you print it out with a %.2f format.

    DECIMAL is an SQL thing. Unless the language has a native decimal type, it
    cannot possibly know how to display it in the same format as your SQL.
     
    Tim Roberts, Jun 14, 2006
    #10
  11. unless your database adapter returns everything as strings...

    </F>
     
    Fredrik Lundh, Jun 14, 2006
    #11
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.