a = 0.9987623
# 将 a 保留两位小数得到
a = 0.99
1
zh0408 2018-08-22 17:09:14 +08:00
round()
|
2
gnozix 2018-08-22 17:14:39 +08:00
format
|
4
gimp 2018-08-22 17:26:01 +08:00 1
>>> r = lambda f: f - f % 0.01
>>> r(2.368) 2.36 >>> r(2.36888888) 2.36 >>> r(2.323) 2.32 >>> r(2.326) 2.32 摘自: https://www.reddit.com/r/learnpython/comments/4nj5gu/how_to_get_float_to_two_decimal_places_without/ |
5
chenxingyu1021 2018-08-22 17:38:15 +08:00 1
int(a*100)/100
|
6
lxy42 2018-08-22 17:39:50 +08:00
Q. In a fixed-point application with two decimal places, some inputs have many places and need to be rounded. Others are not supposed to have excess digits and need to be validated. What methods should be used?
A. The quantize() method rounds to a fixed number of decimal places. If the Inexact trap is set, it is also useful for validation: >>> >>> TWOPLACES = Decimal(10) ** -2 # same as Decimal('0.01') >>> >>> # Round to two places >>> Decimal('3.214').quantize(TWOPLACES) Decimal('3.21') |
8
pyse 2018-08-23 13:43:59 +08:00
你们的为什么没有四舍五入呢???
>>> a = 0.9987623 >>> print("%.2f" %a) 1.00 |