Python, print all floats to 2 decimal places in output

前端 未结 7 1121
野性不改
野性不改 2020-12-07 18:38

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

This is what I have:

print \'%.2f\' % var1,\'kg =\',\'%.2f\' % var2,\'lb =\',\'%.2f\' % va         


        
7条回答
  •  一整个雨季
    2020-12-07 19:06

    Not directly in the way you want to write that, no. One of the design tenets of Python is "Explicit is better than implicit" (see import this). This means that it's better to describe what you want rather than having the output format depend on some global formatting setting or something. You could of course format your code differently to make it look nicer:

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

提交回复
热议问题