syrte
3/20/2016 - 1:47 PM

print required significant figures.

print required significant figures.


def siground(x, n):
    from math import log10, floor
    x, n = float(x), int(n)
    assert n > 0
    if x == 0:
        return ("%%.%if" % (n - 1)) % x
    m = 10 ** floor(log10(abs(x)))
    x = round(x / m, n - 1) * m
    p = floor(log10(abs(x)))
    if -3 < p < n:
        return ("%%.%if" % (n - 1 - p)) % x
    else:
        return ("%%.%ife%%+i" % (n - 1)) % (x / 10**p, p)