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

Float number division with 3 decimal places

So I need to do some division. Does anyone know how to output the result with three decimal places? For example: –>
5/2 = 2.500 8/6 = 1.333 10/3 = 3.333

Tried code:

a = int(input())
b = int(input())
print(a/b)

You can see, if the input is 5 and 2 it will output 2.5(not 3 decimal places)

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

>Solution :

print(f'{a/b:.3f}')

Manual on formatting mini-language : https://docs.python.org/3/library/string.html#formatspec

This will print 2/3 as 0.666, if you want 0.667 use :.3g instead.

  • f = fixed point
  • g = general format
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