Extrange behavior with numeric datatypes and decimal places
Hi list. I'm having a problem using the following: - Gentoo Linux - Zope 2.6.1 - Python 2.1.3 - psycopg 1.1.2 - zpsycopgda 1.1.2 - Postgresql 7.3.3 The situation is I'm trying to get a number (result of a division) rounded to two decimal places and can't get it to work. After some research, my problem can be simplified to this: If I run the query: select 3::numeric(10,2) in psql I get a result of 3.00 (normal and expected behavior) in zope I get a result of 3.0 (why just one decimal place?) How can I get around this. Is this a bug in psycopg or something like that? Anybody experienced this? Thanks in advance.
I've put "select 3::numeric(10,2)" in a zsql method, in a zpt that calls that method and in the test tab of a zpsycopg database connection object getting the same results on all of them. Thanks. Luis. Chris Withers wrote:
Luis Mariano Luporini wrote:
in zope I get a result of 3.0 (why just one decimal place?)
What code are you using that displays this?
cheers,
Chris
Luis Mariano Luporini wrote:
I've put "select 3::numeric(10,2)" in a zsql method, in a zpt that calls that method
This is the code you want to fix. What is the code you currently have? Chris
Chris: That is the code I'm having problems with. If I run that in psql, I get the number of decimal places specified by the numeric casting but, if I run it in a zsql I get just un decimal place regardless of what is specified in the cast operation. As suggested by Neil Smith in the zope-db list I could use a formating mask in dtml or zpt but I think that default behavior should be to left the query as it is. Thanks for your reply. Luis. Chris Withers wrote:
Luis Mariano Luporini wrote:
I've put "select 3::numeric(10,2)" in a zsql method, in a zpt that calls that method
This is the code you want to fix. What is the code you currently have?
Chris
Luis Mariano Luporini wrote:
Chris:
That is the code I'm having problems with.
I know, that's why I asked you to supply it so we can take a look and help you!
If I run that in psql, I get the number of decimal places specified by the numeric casting but, if I run it in a zsql I get just un decimal place regardless of what is specified in the cast operation.
The DA is probably turning the returned number into a python Float, the default representation of which has one decimal place.
As suggested by Neil Smith in the zope-db list I could use a formating mask in dtml or zpt but I think that default behavior should be to left the query as it is.
Then you'd get a string ;-) Most people want numbers when they select numeric columns from relational databases... Chris
You're right but anyway I suppose I'll handle it with formatting options. Thanks Chris. Luis. Chris Withers wrote:
Luis Mariano Luporini wrote:
Chris:
That is the code I'm having problems with.
I know, that's why I asked you to supply it so we can take a look and help you!
If I run that in psql, I get the number of decimal places specified by the numeric casting but, if I run it in a zsql I get just un decimal place regardless of what is specified in the cast operation.
The DA is probably turning the returned number into a python Float, the default representation of which has one decimal place.
As suggested by Neil Smith in the zope-db list I could use a formating mask in dtml or zpt but I think that default behavior should be to left the query as it is.
Then you'd get a string ;-) Most people want numbers when they select numeric columns from relational databases...
Chris
participants (2)
-
Chris Withers -
Luis Mariano Luporini