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

Rounding up to two limits in Python

I am trying to round up to nearest 10 for Max and Min. However, for Max, the nearest 10 should be greater than Max and for Min, the nearest 10 should be less than Min. The current and the expected outputs are presented.

import numpy as np
Max = [99.91540553]
Min = [8.87895014]
Amax=round(Max[0],-1)
Amin=round(Min[0],-1)

The current output is

Amax=100
Amin=10.0

The expected output is

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

Amax=100
Amin=0.0

>Solution :

If I understand your question correctly, you’d need the floor and ceil functions from the math module.

import math as m
Max = [99.91540553]
Min = [8.87895014]
Amax = 10*m.ceil(Max[0]/10)
Amin = 10*m.floor(Min[0]/10)

These also exist in numpy if you would like to perform this on every element in a numpy array:

import numpy as np
Max = np.array([99.91540553, 95.7, 93.2])
Min = np.array([8.87895014, 15.7, 17.2])
Amax = 10*np.ceil(Max/10)
Amin = 10*np.floor(Min/10)
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