worth = Decimal(request.POST.get('worth'))
print(request.user.profile.cash) # -> prints 10000.000000000000000
print(worth) # -> prints 10000
Profile.objects.filter(user=request.user).update(cash=F('cash')-worth)
request.user.profile.refresh_from_db()
if request.user.profile.cash < 0:
###!!!!####
print(request.user.profile.cash) # -> -1.819E-12
#model definition:
class Profile(models.Model):
cash = models.DecimalField(default=10000, max_digits=30, decimal_places=15)可以看出,玩家有10k现金,而我减去10k,结果是负值,我如何解决这个问题?我不允许现金如你所预期的那样为负数。我是不是做错了什么?
我希望这位球员能够全身心投入他的钱。
发布于 2017-09-13 15:08:44
您可以通过从python中的现金中减去值(即不使用数据库查询)来修复这个问题。我非常肯定,为了做到这一点,您必须首先将值转换为Decimal值。这可能是因为Django使用来自Decimal模块的decimal值来对十进制字段值进行算术,这些值对于十进制数字具有完美的精度。我不知道您的数据库在查询时在做什么,但是它必须涉及某种浮点的数学。您所看到的值基本上等于零(-0.000000000001819),但是包含了一些浮点错误。
https://stackoverflow.com/questions/46200813
复制相似问题