Rounding to significant figures in numpy

后端 未结 13 1473
无人及你
无人及你 2020-12-09 16:00

I\'ve tried searching this and can\'t find a satisfactory answer.

I want to take a list/array of numbers and round them all to n significant figures. I have written

13条回答
  •  庸人自扰
    2020-12-09 16:17

    For Scalars

    sround = lambda x,p: float(f'%.{p-1}e'%x)
    

    Example

    >>> print( sround(123.45, 2) )
    120.0
    

    For Arrays

    Use Scott Gigante's signif(x, p) fig1 fig2

提交回复
热议问题