gpt4 book ai didi

python - pyodbc sqlserver游标小数四舍五入

转载 作者:太空宇宙 更新时间:2023-11-03 19:00:34 25 4
gpt4 key购买 nike

我在使用 pyodbc API 中的任何获取方法(本例中为 fetchall)获取结果时遇到了这个问题,小数值四舍五入到两位。在 MSSQL 数据库中,更具体地说,在 SQL Management Studio 中,当我执行存储过程时,余额、借方和贷方的值正确显示到 4 个位置。

我尝试在 fetchall 后格式化结果集,它确实显示了四位小数,但它仍然将其四舍五入。例如 14.5272 => 14.5300。

在模板中,我也尝试使用 floatformat 过滤器,但无济于事,它仍然保持四舍五入:

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

模型.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]

以下是 odbc/freetds 配置文件。我使用 SQL Server 2008。Freetds v1.12、django-pyodbc v0.9 和 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

此外,我的设置文件 (INSTALLED_APPS) 中有该应用程序,并且该项目已正确同步,并且我在所有应用程序中都使用了缓存,如果这很重要的话,我认为这不是此问题的根源。

除了这个舍入问题之外,一切正常,感谢任何帮助。

最佳答案

我想通了。所以我调用的存储过程,浮点字段的类型是MONEY,并且四舍五入到2位。我将其更改为 Decimal(7,4) ,现在一切正常。我没有对 odbc/freetds 配置进行任何更改,也不必使用过滤器 float 格式。再次感谢,小熊

关于python - pyodbc sqlserver游标小数四舍五入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16169298/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com