Python 输出百分比

注:python3环境试验

0x00 使用参数格式化{:2%}

{:.2%}: 显示小数点后2位

print('{:.2%}'.format(10/50))  #percent: 20.00%

 {:.0%}: 不显示小数点

print('{:.0%}'.format(10/50))  #percent: 20%

 

0x01 格式化为float

{:.2f}%:显示小数点后2位

print('{:.2f}%'.format(10/50))  #percent: 20.00%

 {:.0f}%: 不显示小数点

print('{:.0f}%'.format(10/50))  #percent: 20%

 

0x02 补充说明

{ } 的意思是对应format()的一个参数,按默认顺序对应,参数序号从0开始,{0}对应format()的第一个参数,{1}对应第二个参数。例如:

print('test1:{1:.1%}, test2:{0:.1%}'.format(40/50, 40/100))

上面指定了顺序

输出:test2: 40.0%, test1: 80.0%

posted @ 2018-09-14 21:12  Alummox  阅读(721)  评论(0编辑  收藏  举报