Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Limit decimal points in a float

I am trying to limit my decimal places to 2 while printing the value of Pi.

Below is my expression after installing the match module in Anaconda Notebook

x=math.pi

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

print(float(x) (%2f))

Could you help fix the code/expression to print 3.14

>Solution :

You are looking for

x = math.pi
print(f"{x:.2f}")

This is called an f-string. It formats x as a string of a floating point number (f) with 2 decimal places (.2f).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

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

Continue reading