Python,将所有浮点数输出到小数点后两位 [英] Python, print all floats to 2 decimal places in output

查看:1571
本文介绍了Python,将所有浮点数输出到小数点后两位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是我的:

<$> p $ p> print'%.2f'%var1,'kg =','%。2f'%var2,'lb =','%。2f'%var3,'gal =', '%.2f'%var4,'l'

这是非常不洁的,看起来很糟糕。有没有一种方法可以使任何浮动输出'%.2f'?



注意:使用Python 2.6。

解决方案

清理它如下:

  print%.2f kg =%.2f lb =%.2f gal =%.2f l%(var1,var2,var3,var4)


I need to output 4 different floats to two decimal places.

This is what I have:

print '%.2f' % var1,'kg =','%.2f' % var2,'lb =','%.2f' % var3,'gal =','%.2f' % var4,'l'

Which is very unclean, and looks bad. Is there a way to make any float in that out put '%.2f'?

Note: Using Python 2.6.

解决方案

Well I would atleast clean it up as follows:

print "%.2f kg = %.2f lb = %.2f gal = %.2f l" % (var1, var2, var3, var4)

这篇关于Python,将所有浮点数输出到小数点后两位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆