Skip to content

Python float precision

  • by

There are many ways to set the precision of the floating-point values in Python. Here are some Python float precision methods:

  • Using “%” operator:- It format as well as set precision in python. This is similar to the “printf” statement in C programming.
  •  format():- This format is the string for setting precision. 
  • round(x,n):- Used to decimal part rounded.

Python float precision example

Simple example code.

x = 3.4536

print('%.2f' % x)

print("{0:.3f}".format(x))

print(round(x, 2))

Output:

Python float precision

Python float precision with no integer precision after the decimal point, The % operator has the following behavior.

>>> "%6.*f" % (2, 1.234)
'  1.23'

Do comment if you have any doubts or suggestions on this Python float topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading