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

Python Rounding (Properly)

For numbers like 10.5, round(10.5) gives 10. 5 should round up, not down. Is there a better built-in function.

Note:

    > .5 - Round up
    < .5 - Round down

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 :

Python uses "banker’s rounding" for x.5. Even numbers round down, odd numbers round up. You are saying it "should" round up, but that’s just your definition. It’s not the only definition.

If you always want rounding up, do:

result = int(x+0.5)
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