Question

I am getting this issue where I fetch results using (fetchall in this example) any of the fetch methods in the pyodbc API, the decimal values get rounded to two places. In the MSSQL database, more particularly in the SQL management Studio when I EXEC the stored procedure the values for balance, debits and credits show up correctly to 4 places.

I tried formatting the resultset after I fetchall and it does display the four decimal places but it still rounds it up though. For example 14.5272 => 14.5300.

In the template also I tried using the floatformat filter to no avail it still keeps rounding:

<td >{{ Accounts.debits|floatformat:4 }}</td>

models.py

from django.core.cache import cache
from django.conf import settings  

class Account(models.Model):                                                                                               
    account = models.IntegerField(primary_key=True, db_column='Account')
    description = models.TextField(db_column='Description')                      
    balance = models.DecimalField(decimal_places=4, max_digits=19, db_column='Balance') 
    debits = models.DecimalField(decimal_places=4, max_digits=19, db_column='Debits')
    credits = models.DecimalField(decimal_places=4, max_digits=19, db_column='Credits')                                                           

class Meta:                                                                                                                       
    db_table = 'Account'                                                                                               
    permissions = (("view_accounts", "Can view accounts"),                                                  
    )                                                                                                                             

    @staticmethod                                                                                                                     
   def resultSet(startDate, endDate):                                                                                                
       try:                                                                                                 
           cur = connection.cursor()                                                                                                                                                                                                                                      
           cacheKey = 'Account_%s_%s' %(startDate, endDate)                                                                        
           cachedSp = cache.get(cacheKey)                                                                                            
           print cacheKey                                                                                                            
           if not cachedSp:                                                                                                          
               print 'not cached'                                                                                                    
               cur.execute('database.dbo.AccountsSelect %s, %s', (str(startDate), str(endDate)))               
               cachedSp = cur.fetchall()                                                                                             
               #for row in cachedSp:                                                                                                  
                   #if row[5]!= -1:                                                                                                   
                       #print format(row['debit'],'%4f')                                                                              
               cachedSp = cur.fetchall()                                                                                             
               cur.close()                          

               cache.set(cacheKey,cachedSp,settings.CACHE_CUSTOM_APP_SECONDS)                                                        
       except Exception, e:                                                                                                          
           print e.args                                                                                                              

       return [Account(*row) for row in cachedSp]    

Following are the odbc/freetds config files. I use SQL server 2008. Freetds v1.12, django-pyodbc v0.9 and pyodbc v3.0.6.

freetds.conf

[global]
    # TDS protocol version
    ; tds version = 8.0
    # Whether to write a TDSDUMP file for diagnostic purposes
    # (setting this to /tmp is insecure on a multi-user system)
    ;   dump file = /tmp/freetds.log
    ;   debug flags = 0xffff

    # Command and connection timeouts
    ;   timeout = 10
    ;   connect timeout = 10

    # If you get out-of-memory errors, it may mean that your client
    # is trying to allocate a huge buffer for a TEXT field.
    # Try setting 'text size' to a more reasonable limit
    text size = 64512

[tes-db-1]
    host = tes-db-1.doamin.test.com
    port = 1433
    tds version = 8.0

odbc.ini

[default]
Driver = FreeTDS
Description = ODBC connection via FreeTDS
Trace = No
Servername = SERVER
Database = DATABASE

odbcinist.ini

[FreeTDS]
Description      = TDS Driver (Sybase / MS-SQL)
Driver           = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Setup            = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so
CPTimeout        =
CPReuse          =
FileUsage        = 1
client charset   = utf-8

Also, I have the app in my settings file (INSTALLED_APPS) and the project synced up correctly and I use caching in all my apps and I don't think is the source of this issue, if that should matter.

Other than this rounding issue everything is working fine, any help is appreciated.

Was it helpful?

Solution

I figured it out.So the stored procedure that I was calling, the float fields were of type MONEY and were getting rounded to 2 places. I changed it to Decimal(7,4) and now everything works fine. I did not make any changes to the odbc/freetds configuration nor did I have to use the filter float format. Thanks again, beargle

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top