我有一个小函数,它舍入十进制数,我的函数如下:
from decimal import Decimal, ROUND_UP
def convert_decimal(decimal_number):
return decimal_number.quantize(Decimal('0.01'), rounding=ROUND_UP)
当我运行django项目时,它在我的代码中正常工作,没有任何问题。该功能正常运行。
但是当我尝试下面的代码时,它失败并出现奇怪的错误:
class UtilityFunctionsTests(TestCase):
def test_decimal_number(self):
decimal_number = Decimal("23.54646354745375634756868")
converted = convert_decimal(decimal_number)
self.assertEqual(converted, Decimal("23.55"))
失败的错误如下:
integration/tests/test_base_updater.py:78:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
decimal_number = Decimal('23.546463547453758025085335248149931430816650390625')
def convert_decimal(decimal_number):
> return decimal_number.quantize(Decimal('0.01'), rounding=ROUND_UP)
E decimal.InvalidOperation: [<class 'decimal.InvalidOperation'>]
entegrasyon/tests/test_base_updater.py:11: InvalidOperation