If we have a number -2.0, -1.0, 0.0, 1.0, 2.0, 3.0 etc, can we remove the floats with minimal usage of if statements? I know that we can round down, but it wouldn't work in case we get a float like 1.2435532, or something similar.

Note: Note that the list of floats with zeros include negatives.

You can use this

def nearest_int(afloat):
    return int(afloat + (0.5 if afloat > 0 else -0.5))
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.