你可以使用负数舍入整数:
>>> round(1234, -3)1000.0
因此,如果你只需要最高有效数字:
>>> from math import log10, floor>>> def round_to_1(x):... return round(x, -int(floor(log10(abs(x)))))... >>> round_to_1(0.0232)0.02>>> round_to_1(1234243)1000000.0>>> round_to_1(13)10.0>>> round_to_1(4)4.0>>> round_to_1(19)20.0
如果大于1,则可能需要将float转换为整数。



