常见计算 格式化数据
百分数
def convert_percent(i: float) -> tuple[str, str]:
def f(i: float) -> str:
'''
四十五入,百分数,2位
0.12345 12.35%
0.12344 12.34%
:param i:
:return:
'''
return ''.join(('%.2f' % i, '%'))
# 0.12345 ('12.35%', '87.66%')
# 0.12344 ('12.34%', '87.66%')
a, b = f(i * 100), f(100 - i * 100)
if a[-2] == '5':
# 0.12345 ('12.35%', '87.66%') -> ('12.35%', '87.65%')
b = b[0:-2] + '5%'
return a, b