Wednesday, October 16, 2013

sigfigs

def _sigfigs(n, sigfigs=3):
    'helper function to round a number to significant figures'
    if n == 0 or math.isnan(n):  # avoid math domain errors
        return n
    return round(float(n), -int(math.floor(math.log10(abs(n))) - sigfigs + 1))

2 comments: