Decimals

T

Tgone

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
 
L

Laszlo Nagy

Tgone írta:
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...
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
 
T

Tgone

Laszlo said:
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?

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.
 
S

Sybren Stuvel

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

That's the same thing, isn't it? 15.0 == 15.000000000
Here's my code:

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

Try string formatting:

print '%.2f' % product.price

Sybren
 
T

Tgone

Sybren said:
Tgone enlightened us with:

That's the same thing, isn't it? 15.0 == 15.000000000

Yes, they're both mathematically the same. I never said they weren't...
Try string formatting:

print '%.2f' % product.price

That works. I expected Python to display the data exactly as it is in
the database, like most languages.
 
L

Laszlo Nagy

That works. I expected Python to display the data exactly as it is in
the database, like most languages.
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
 
S

Sybren Stuvel

Tgone enlightened us with:
Yes, they're both mathematically the same. I never said they
weren't...

You did act like they weren't.
That works. I expected Python to display the data exactly as it is
in the database, like most languages.

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
 
S

Steve Holden

Tgone said:
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.
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
 
D

Diez B. Roggisch

Tgone said:
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.

Care to give us an example of "one of those"?

Diez
 
T

Tim Roberts

Tgone said:
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.

"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.
 
F

Fredrik Lundh

Tim said:
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.

unless your database adapter returns everything as strings...

</F>
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top