def round_fig(x, n=1, retint=False):
"""
Rounds x at the n-th figure. n must be >1
ex: 1234.567 with n=3-> 1230.0
"""
if np.iterable(x):
x = np.asarray(x).copy()
ff = (x != 0)
dd = 10**(np.floor(np.log10(np.abs(x[ff])))-n+1)
x[ff] = np.round(x[ff]/dd)
if not retint:
x[ff] *= dd
return x
elif x != 0:
dd = 10**(np.floor(np.log10(np.abs(x)))-n+1)
x = np.round(x/dd)
if not retint:
x *= dd
return x
else:
return x
评论列表
文章目录