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

How to round off a floating number to one decimal place from the print line python function directly

So I have the following code

for x in range(10):
  
    print(f'Machine Busy Val     : {read_log ("Machine_busy_value")}')

read_log is the function and it is getting the machine_busy_value. it is currently returning a floating number but I would like it to return the value in 2 decimal places. How can I go about that? Any type of suggestion would be appreciated. I am using python 2.7 if that helps

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 :

You might consider using format()

print ("{val:.1f}".format(val=read_log("Machine_busy_value") ))

That’s python 2.7 (which doesn’t have f-strings). There are plenty of questions like this already out there

If you’re on python 3.7, then you can just end your read_log call in the string with the :.2f to format it.

f'Machine Busy Val     : {read_log ("Machine_busy_value"):.1f}'
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