From: <se...@en...> - 2005-02-24 11:09:12
|
Hi! I have some problem, when I use Sybase module with GTK: cursor.execute(" select 1.2 ") num = cursor.fetchone()[0] print num # 1.2 print float(1.2) # 1.2 print float(num) # 1.2 import gtk print num # 1.2 print float(1.2) # 1.2 print float(num) # 1.0 <- not 1.2 After import gtk, the float truncates the decimal. How can I fix this? Thanks, Norbert Sebok |
From: Andrew M. <an...@ob...> - 2005-02-24 17:05:42
|
>I have some problem, when I use Sybase module with GTK: > > cursor.execute(" select 1.2 ") > num = cursor.fetchone()[0] > print num # 1.2 > print float(1.2) # 1.2 > print float(num) # 1.2 > > import gtk > print num # 1.2 > print float(1.2) # 1.2 > print float(num) # 1.0 <- not 1.2 > > >After import gtk, the float truncates the decimal. >How can I fix this? This is a shot in the dark, but could gtk be setting the locale? It might also be useful to print the type of num, eg: print type(num) -- Andrew McNamara, Senior Developer, Object Craft http://www.object-craft.com.au/ |
From: Marcos <ma...@bu...> - 2005-02-25 05:47:37
|
Probably related to this: This is one of the new things in Python 2.4. Now there is a locale-independent (C locale) way of treating numbers. I bet the module has to be adapted to this change to take advantage of this fact (and letting users choose their own locale without breaking apps). http://www.python.org/doc/2.4/whatsnew/node11.html Andrew McNamara dijo: >>I have some problem, when I use Sybase module with GTK: >> >> cursor.execute(" select 1.2 ") >> num = cursor.fetchone()[0] >> print num # 1.2 >> print float(1.2) # 1.2 >> print float(num) # 1.2 >> >> import gtk >> print num # 1.2 >> print float(1.2) # 1.2 >> print float(num) # 1.0 <- not 1.2 >> >> >>After import gtk, the float truncates the decimal. >>How can I fix this? > > This is a shot in the dark, but could gtk be setting the locale? > > It might also be useful to print the type of num, eg: > > print type(num) > |
From: <se...@en...> - 2005-02-25 11:51:05
|
This locale feature is started on 19-Jul-2003 in PEP 331, the latest Sybase module is created on 27-Apr-2003. I think it's not use this feature yet. Anyway, I wrote my own converter. It's ugly solution and maybe slow, but it's works... def sybase_num_to_python_float(sybase_num): if sybase_num == None: return None elif not '.' in str(sybase_num): return int(sybase_num) else: if '-' in str(sybase_num): negative = True whole,decimal = str(sybase_num).replace('-','').split('.') else: negative = False whole,decimal = str(sybase_num).split('.') value = float(whole) + float(decimal) / (10 ** len(decimal)) if negative: value = - value return value Norbert Sebok >Probably related to this: > >This is one of the new things in Python 2.4. Now there is a >locale-independent (C locale) way of treating numbers. I bet the module >has to be adapted to this change to take advantage of this fact (and >letting users choose their own locale without breaking apps). > >http://www.python.org/doc/2.4/whatsnew/node11.html > >Andrew McNamara dijo: > > >>>I have some problem, when I use Sybase module with GTK: >>> >>> cursor.execute(" select 1.2 ") >>> num = cursor.fetchone()[0] >>> print num # 1.2 >>> print float(1.2) # 1.2 >>> print float(num) # 1.2 >>> >>> import gtk >>> print num # 1.2 >>> print float(1.2) # 1.2 >>> print float(num) # 1.0 <- not 1.2 >>> >>> >>>After import gtk, the float truncates the decimal. >>>How can I fix this? >>> >>> >>This is a shot in the dark, but could gtk be setting the locale? >> >>It might also be useful to print the type of num, eg: >> >> print type(num) >> >> |
From: Andrew M. <an...@ob...> - 2005-02-25 17:27:57
|
>Probably related to this: > >This is one of the new things in Python 2.4. Now there is a >locale-independent (C locale) way of treating numbers. I bet the module >has to be adapted to this change to take advantage of this fact (and >letting users choose their own locale without breaking apps). > >http://www.python.org/doc/2.4/whatsnew/node11.html It's certainly related, but I don't think the 2.4 changes were the cause. What is almost certainly happening is that GTK is changing the locale, which effects the conversion within the python-sybase modules C code. If python-sybase used the new interface (when available), that would hopefully eliminate this problem. Someone who uses locales want to find the offending conversions and post a patch? -- Andrew McNamara, Senior Developer, Object Craft http://www.object-craft.com.au/ |