Skip to content
Advertisement

Round to the next tens in Python

Is there a way to round always up to the next tens? E.g., 0.000000000003 shall become 0.1. 0.1244593249234 shall become 0.2 and 0.9x shall become 1. Negative numbers are not a thing here.

Is there a built in or do I need to come up with something?

Advertisement

Answer

by multiplying the input f to 10, you will map it from [0, 1] to [0, 10]. Now by getting ceil from this number, you will get an integer number in [0, 10] that by dividing it by 10, you will obtain the next ten in [0, 1] range as you desire:

import math

next_ten = math.ceil(f * 10.0) / 10.0
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement