Moderators / float


what is 2f used with moderators? :shushing_face:


“print” treats the % as a special character you need to add, so it can know, that when you type “f”, the number (result) that will be printed will be a floating point type, and the “.2” tells your “print” to print only the first 2 digits after the point.


Not quite. It does not print the first two digits after the point, but rather it rounds the float to two digits. The rounding could be up or down, depending whether the third digit is a 5 or more, or less than 5, respectively.

>>> from math import pi
>>> PI = pi
>>> print "PI to 5 decimal places %.5f" % PI
PI to 5 decimal places 3.14159
>>> print "PI to 4 decimal places %.4f" % PI
PI to 4 decimal places 3.1416
>>> print "PI to 3 decimal places %.3f" % PI
PI to 3 decimal places 3.142
>>> print "PI to 2 decimal places %.2f" % PI
PI to 2 decimal places 3.14
>>> print "PI to 1 decimal place %.1f" % PI
PI to 1 decimal place 3.1


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.