python saved two decimal places

 First, the code

import decimal

decimal.getcontext().rounding = decimal.ROUND_HALF_UP


def index(number):
    n = str(number*100)
    n = decimal.Decimal(n).__round__(1)
    n = n / decimal.Decimal("100")
    n = decimal.Decimal(str(n), decimal.getcontext())
    return float(n.__round__(2))


count = 0.015
while count < 1:
    r = index(count)
    print(r, str(count), sep=' ----- ')
    count += 0.01

 Second, the results

 

Guess you like

Origin www.cnblogs.com/dream4567/p/11015719.html