Python, print all floats to 2 decimal places in output

前端 未结 7 1178
野性不改
野性不改 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 18:46

    If you are looking for readability, I believe that this is that code:

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

提交回复
热议问题