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 can I limit to 5 digits a Python variable (float type)?

I have the variable

current_price = 1.158805

But I need to limit to 5 numbers (in this case I want
the current price to be 1.15880), what can I do?

I need a general solution because the variable changes every second.

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

And what if I have the numer in a pandas dataframe, for example data.close.iloc[-1]?

>Solution :

 current_price = 1.158805
 
 print(str(current_price)[:-1])

Your output will be:

 >>> 1.15880
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