使用Python 2.7,如何将我的数字四舍五入到两位小数,而不是默认的十位或更多位?
print "financial return of outcome 1 =","$"+str(out1)
使用Python 2.7,如何将我的数字四舍五入到两位小数,而不是默认的十位或更多位?
print "financial return of outcome 1 =","$"+str(out1)
round()
:>>> round(1.2345,2)
1.23
>>> round(1.5145,2)
1.51
>>> round(1.679,2)
1.68
或内置函数format()
:
>>> format(1.2345, '.2f')
'1.23'
>>> format(1.679, '.2f')
'1.68'
或者新的格式化字符串:
>>> "{:.2f}".format(1.2345)
'1.23
>>> "{:.2f}".format(1.679)
'1.68'
或者旧式字符串格式化:
>>> "%.2f" % (1.679)
'1.68'
关于round
的帮助:
>>> print round.__doc__
round(number[, ndigits]) -> floating point number
Round a number to a given precision in decimal digits (default 0 digits).
This always returns a floating point number. Precision may be negative.
Decimal("{:.2f}".format(val))
- Patch Rick WalshDecimal(format(val, '.2f'))
。 - Ashwini ChaudharyDecimal('123.345').quantize(Decimal('1.00'), rounding=decimal.ROUND_HALF_UP)
会得到 Decimal('123.35')
。另一方面,Decimal(format(Decimal('123.345'), '.2f'))
会得到 Decimal('123.34')
,因为123.345的二进制表示小于123.345。 - Patch Rick Walsh因为你谈论的是财务数字,所以不应该使用浮点数算术。最好使用 Decimal。
>>> from decimal import Decimal
>>> Decimal("33.505")
Decimal('33.505')
使用新风格的format()
进行文本输出格式化(默认为半偶数舍入):
>>> print("financial return of outcome 1 = {:.2f}".format(Decimal("33.505")))
financial return of outcome 1 = 33.50
>>> print("financial return of outcome 1 = {:.2f}".format(Decimal("33.515")))
financial return of outcome 1 = 33.52
由于浮点数不精确导致的四舍五入差异如下所示:
>>> round(33.505, 2)
33.51
>>> round(Decimal("33.505"), 2) # This converts back to float (wrong)
33.51
>>> Decimal(33.505) # Don't init Decimal from floating-point
Decimal('33.50500000000000255795384873636066913604736328125')
正确的财务数值取舍方式:
>>> Decimal("33.505").quantize(Decimal("0.01")) # Half-even rounding by default
Decimal('33.50')
在不同的交易中,还常见其他类型的四舍五入:
>>> import decimal
>>> Decimal("33.505").quantize(Decimal("0.01"), decimal.ROUND_HALF_DOWN)
Decimal('33.50')
>>> Decimal("33.505").quantize(Decimal("0.01"), decimal.ROUND_HALF_UP)
Decimal('33.51')
请记住,如果您正在模拟回报结果,则可能需要在每个利息期间进行舍入,因为您无法支付/接收分数的分数,也无法获得分数的利息。对于模拟来说,使用浮点数是相当常见的,但如果这样做,请始终记住存在误差。因此,即使是固定利率的投资,由于此原因而略有不同。
您也可以使用str.format()
:
>>> print "financial return of outcome 1 = {:.2f}".format(1.23456)
financial return of outcome 1 = 1.23
当使用分/整数时,您会遇到115(例如1.15美元)和其他数字的问题。
我有一个将整数转换为浮点数的函数。
...
return float(115 * 0.01)
那个函数通常可以正常工作,但有时会返回类似于 1.1500000000000001
的结果。
所以我改变了我的函数,使其返回如下...
...
return float(format(115 * 0.01, '.2f'))
这将返回1.15
,而不是'1.15'
或1.1500000000000001
(返回浮点数而非字符串)。
我大多数时候发帖是为了记住在这种情况下我所做的事情,因为这是谷歌中的第一个结果。
我认为最好的方法是使用format()函数:
>>> print("financial return of outcome 1 = $ " + format(str(out1), '.2f'))
// Should print: financial return of outcome 1 = $ 752.60
但我必须说:在处理金融价值时不要使用 round 或 format。
format(out1, '.2f')
,不需要将其转换为字符串。 - danius当我们使用round()函数时,它可能不会给出正确的值。
你可以通过以下方式进行检查, round(2.735)和round(2.725)
请使用
import math
num = input('Enter a number')
print(math.ceil(num*100)/100)
print "financial return of outcome 1 = $%.2f" % (out1)
def roundup(x):
return round(int(math.ceil(x / 0.05)) * 0.05,2)
>>> out1 = 1.2345
>>> out1 = float(str(out1)[0:4])
>>> out1
可能不是超级高效的,但简单易用 :)
Decimal
。请注意,Decimal
是Python库中的一个数据类型,可用于精确计算小数并避免浮点数舍入误差。 - Tim Pietzcker