# 如何将 Python 中的数字舍入为有效数字

#### How to round a number to significant figures in Python

``````1234 -> 1000

0.12 -> 0.1

0.012 -> 0.01

0.062 -> 0.06

6253 -> 6000

1999 -> 2000
``````

26 个回复

``````>>> 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
``````

``````>>> '%s' % float('%.1g' % 1234)
'1000'
>>> '%s' % float('%.1g' % 0.12)
'0.1'
>>> '%s' % float('%.1g' % 0.012)
'0.01'
>>> '%s' % float('%.1g' % 0.062)
'0.06'
>>> '%s' % float('%.1g' % 6253)
'6000.0'
>>> '%s' % float('%.1g' % 1999)
'2000.0'
``````

``````>>> from math import log10, floor
>>> def round_sig(x, sig=2):
...   return round(x, sig-int(floor(log10(abs(x))))-1)
...
>>> round_sig(0.0232)
0.023
>>> round_sig(0.0232, 1)
0.02
>>> round_sig(1234243, 3)
1230000.0
``````
``````f'{float(f"{i:.1g}"):g}'
# Or with Python <3.6,
'{:g}'.format(float('{:.1g}'.format(i)))
``````

1. 正好解决了 OP 问题
2. 不需要任何额外
3. 不需要任何用户定义的辅助功能数学运算

``````print('{:g}'.format(float('{:.{p}g}'.format(i, p=n))))
``````

``````a = [1234, 0.12, 0.012, 0.062, 6253, 1999, -3.14, 0., -48.01, 0.75]
b = ['{:g}'.format(float('{:.1g}'.format(i))) for i in a]
# b == ['1000', '0.1', '0.01', '0.06', '6000', '2000', '-3', '0', '-50', '0.8']
``````

``````>>> round_to_1(1234243)
1000000.0
``````

``````>>> from to_precision import to_precision
>>> to_precision(1234243, 1, 'std')
'1000000'
>>> to_precision(1234243, 1, 'sci')
'1e6'
>>> to_precision(1234243, 1, 'eng')
'1e6'
``````

``````>>> to_precision(599, 2)
'600'
>>> to_precision(1164, 2)
'1.2e3'
``````

``````import math

def signif(x, digits=6):
if x == 0 or not math.isfinite(x):
return x
digits -= math.ceil(math.log10(abs(x)))
return round(x, digits)
``````

``````0.075.hex()
``````

``````0x13333333333333 / 16**13 * 2**-4
``````

``````from decimal import Decimal

Decimal(0x13333333333333) / 16**13 / 2**4
``````

` `from math import log10, floor def round_int(i,places): if i == 0: return 0 isign = i/abs(i) i = abs(i) if i < 1: return 0 max10exp = floor(log10(i)) if max10exp+1 < places: return i sig10pow = 10**(max10exp-places+1) floated = i*1.0/sig10pow defloated = round(floated)*sig10pow return int(defloated*isign)``

``````from math import log10, floor
def round_sig(x, sig=6, small_value=1.0e-9):
return round(x, sig - int(floor(log10(max(abs(x), abs(small_value))))) - 1)
``````
``````def round_to_n(x, n):
if not x: return 0
power = -int(math.floor(math.log10(abs(x)))) + (n - 1)
factor = (10 ** power)
return round(x * factor) / factor

round_to_n(0.075, 1)      # 0.08
round_to_n(0, 1)          # 0
round_to_n(-1e15 - 1, 16) # 1000000000000001.0
``````

``````round_to_n(1e15 + 1, 11)  # 999999999999999.9
``````

``````>>> round(1.2322, 2)
1.23
``````

``````>>> def intround(n, sigfigs):
...   n = str(n)
...   return n[:sigfigs] + ('0' * (len(n)-(sigfigs)))

>>> intround(1234, 1)
'1000'
>>> intround(1234, 2)
``````

``````>>> def roundall1(n, sigfigs):
...   n = str(n)
...   try:
...     sigfigs = n.index('.')
...   except ValueError:
...     pass
...   return intround(n, sigfigs)
``````

``````>>> def roundall2(n, sigfigs):
...   if type(n) is int: return intround(n, sigfigs)
...   else: return round(n, sigfigs)
``````

``````import sys,math

def tidy(x, n):
"""Return 'x' rounded to 'n' significant digits."""
y=abs(x)
if y <= sys.float_info.min: return 0.0
return round( x, int( n-math.ceil(math.log10(y)) ) )
``````

``````sys.float_info.min*sys.float_info.epsilon
``````

numpy.format_float_positional直接支持所需的行为。 以下片段返回格式化为 4 位有效数字的浮点数`x` ，科学记数法被抑制。

``````import numpy as np
x=12345.6
np.format_float_positional(x, precision=4, unique=False, fractional=False, trim='k')
> 12340.
``````

sigfig包/库涵盖了这一点。 安装后，您可以执行以下操作：

``````>>> from sigfig import round
>>> round(1234, 1)
1000
>>> round(0.12, 1)
0.1
>>> round(0.012, 1)
0.01
>>> round(0.062, 1)
0.06
>>> round(6253, 1)
6000
>>> round(1999, 1)
2000
``````

``````>>> "{0}".format(float("{0:.1g}".format(1216)))
'1000.0'
>>> "{0}".format(float("{0:.1g}".format(0.00356)))
'0.004'
``````

``````import decimal
from math import log10, floor

def myrounding(value , roundstyle='ROUND_HALF_UP',sig = 3):
roundstyles = [ 'ROUND_05UP','ROUND_DOWN','ROUND_HALF_DOWN','ROUND_HALF_UP','ROUND_CEILING','ROUND_FLOOR','ROUND_HALF_EVEN','ROUND_UP']

power =  -1 * floor(log10(abs(value)))
value = '{0:f}'.format(value) #format value to string to prevent float conversion issues
divided = Decimal(value) * (Decimal('10.0')**power)
roundto = Decimal('10.0')**(-sig+1)
if roundstyle not in roundstyles:
print('roundstyle must be in list:', roundstyles) ## Could thrown an exception here if you want.
return_val = decimal.Decimal(divided).quantize(roundto,rounding=roundstyle)*(decimal.Decimal(10.0)**-power)
nozero = ('{0:f}'.format(return_val)).rstrip('0').rstrip('.') # strips out trailing 0 and .
return decimal.Decimal(nozero)

for x in list(map(float, '-1.234 1.2345 0.03 -90.25 90.34543 9123.3 111'.split())):
print (x, 'rounded UP: ',myrounding(x,'ROUND_UP',3))
print (x, 'rounded normal: ',myrounding(x,sig=3))
``````

``````def smart_round(x, decimal_positions):
dp = - int(math.log10(abs(x))) if x != 0.0 else int(0)
return round(float(x), decimal_positions + dp if dp > 0 else decimal_positions)
``````

https://stackoverflow.com/users/1391441/gabriel ，以下内容是否解决了您对 rnd(.075, 1) 的担忧？ 警告：以浮点数形式返回值

``````def round_to_n(x, n):
fmt = '{:1.' + str(n) + 'e}'    # gives 1.n figures
p = fmt.format(x).split('e')    # get mantissa and exponent
# round "extra" figure off mantissa
p[0] = str(round(float(p[0]) * 10**(n-1)) / 10**(n-1))
return float(p[0] + 'e' + p[1]) # convert str to float

>>> round_to_n(750, 2)
750.0
>>> round_to_n(750, 1)
800.0
>>> round_to_n(.0750, 2)
0.075
>>> round_to_n(.0750, 1)
0.08
>>> math.pi
3.141592653589793
>>> round_to_n(math.pi, 7)
3.141593
``````

``````def sigfig(x, num_sigfig):
num_decplace = num_sigfig - int(math.floor(math.log10(abs(x)))) - 1
return '%.*f' % (num_decplace, round(x, num_decplace))
``````

``````import numpy as np

number=-456.789
significantFigures=4

roundingFactor=significantFigures - int(np.floor(np.log10(np.abs(number)))) - 1
rounded=np.round(number, roundingFactor)

string=rounded.astype(str)

print(string)
``````

``````if roundingFactor<=0:
rounded=rounded.astype(int)
``````

``````import math

def sig_dig(x, n_sig_dig):
num_of_digits = len(str(x).replace(".", ""))
if n_sig_dig >= num_of_digits:
return x
n = math.floor(math.log10(x) + 1 - n_sig_dig)
result = round(10 ** -n * x) * 10 ** n
return float(str(result)[: n_sig_dig + 1])

>>> sig_dig(1234243, 3)
>>> sig_dig(243.3576, 5)

1230.0
243.36
``````

``````def sig_fig_round(number, digits=3):
power = "{:e}".format(number).split('e')[1]
return round(number, -(int(power) - digits))
``````

``````from decimal import Decimal

def to_significant_figures(v: float, n_figures: int) -> str:
d = Decimal(v)
d = d.quantize(Decimal((0, (), d.adjusted() - n_figures + 1)))
return str(d.quantize(Decimal(1)) if d == d.to_integral() else d.normalize())
``````

``````>>> to_significant_figures(1.234567, 3)
'1.23'
>>> to_significant_figures(1234567, 3)
'1230000'
>>> to_significant_figures(1.23, 7)
'1.23'
>>> to_significant_figures(123, 7)
'123'
``````

``````from math import floor

def significant_arithmetic_rounding(n, d):
# Checking whether the no. is negative or positive. If it is negative we will take the absolute value of it and proceed
neg_flag = 0
if n < 0:
neg_flag = 1
n = abs(n)

n1 = n
# Counting the no. of digits to the left of the decimal point in the no.
ld = 0
while(n1 >= 1):
n1 /= 10
ld += 1

n1 = n
# Counting the no. of zeros to the right of the decimal point and before the first significant digit in the no.
z = 0
if ld == 0:
while(n1 <= 0.1):
n1 *= 10
z += 1

n1 = n
# No. of digits to be considered after decimal for rounding
rd = (d - ld) + z
n1 *= 10 ** rd

# Increase by 0.5 and take the floor value for rounding
n1 = floor(n1+0.5)

# Placing the decimal point at proper position
n1 /= 10 ** rd

# If the original number is negative then make it negative
if neg_flag == 1:
n1 = 0 - n1

return n1
``````

``````>>> significant_arithmetic_rounding(1234, 3)
1230.0
>>> significant_arithmetic_rounding(123.4, 3)
123.0
>>> significant_arithmetic_rounding(0.0012345, 3)
0.00123
>>> significant_arithmetic_rounding(-0.12345, 3)
-0.123
>>> significant_arithmetic_rounding(-30.15345, 3)
-30.2
``````

``````def sigfiground(number:float, ndigits=3)->float:
return float(f"{number:.{ndigits}g}")
``````

``````import math

# Given x as float or decimal, returns as string a number rounded to "sig" significant digts
# Return as string in order to control significant digits, could be a float or decimal
def round_sig(x, sig=2):
r = round(x, sig-int(math.floor(math.log10(abs(x))))-1)
floatsig = "%." + str(sig) + "g"
return "%d"%r if abs(r) >= 10**(sig-1) else '%s'%float(floatsig % r)

>>> a = [1234, 123.4, 12.34, 1.234, 0.1234, 0.01234, 0.25, 1999, -3.14, -48.01, 0.75]
>>> [print(i, "->", round_sig(i,1), round_sig(i), round_sig(i,3), round_sig(i,4)) for i in a]

1234 -> 1000 1200 1230 1234
123.4 -> 100 120 123 123.4
12.34 -> 10 12 12.3 12.34
1.234 -> 1 1.2 1.23 1.234
0.1234 -> 0.1 0.12 0.123 0.1234
0.01234 -> 0.01 0.012 0.0123 0.01234
0.25 -> 0.2 0.25 0.25 0.25
1999 -> 2000 2000 2000 1999
-3.14 -> -3 -3.1 -3.14 -3.14
-48.01 -> -50 -48 -48.0 -48.01
0.75 -> 0.8 0.75 0.75 0.75
``````

``````import numpy as np

def Sig_Digit(x, *N,):
if abs(x) < 1.0e-15:
return(1)
N = 1 if N ==() else N[0]
k = int(round(abs(N)-1))-int(np.floor(np.log10(abs(x))))
return(k);

def Sig_Format(x, *Error,):
if abs(x) < 1.0e-15:
return('{}')
Error = 1 if Error ==() else abs(Error[0])
k = int(np.floor(np.log10(abs(x))))
z = x/10**k
k = -Sig_Digit(Error, 1)
m = 10**k
y = round(x*m)/m
if k < 0:
k = abs(k)
if z >= 9.5:
FMT = '{:'+'{}'.format(1+k)+'.'+'{}'.format(k-1)+'f}'
else:
FMT = '{:'+'{}'.format(2+k)+'.'+'{}'.format(k)+'f}'
elif k == 0:
if z >= 9.5:
FMT = '{:'+'{}'.format(1+k)+'.0e}'
else:
FMT = '{:'+'{}'.format(2+k)+'.0f}'
else:
FMT = '{:'+'{}'.format(2+k)+'.'+'{}'.format(k)+'e}'
return(FMT)

def Sci_Format(x, *N):
if abs(x) < 1.0e-15:
return('{}')
N = 1 if N ==() else N[0]
N = int(round(abs(N)-1))
y = abs(x)
k = int(np.floor(np.log10(y)))
z = x/10**k
k = k-N
m = 10**k
y = round(x/m)*m
if k < 0:
k = abs(k)
if z >= 9.5:
FMT = '{:'+'{}'.format(1+k)+'.'+'{}'.format(k-1)+'f}'
else:
FMT = '{:'+'{}'.format(2+k)+'.'+'{}'.format(k)+'f}'
elif k == 0:
if z >= 9.5:
FMT = '{:'+'{}'.format(1+k)+'.0e}'
else:
FMT = '{:'+'{}'.format(2+k)+'.0f}'
else:
FMT = '{:'+'{}'.format(2+N)+'.'+'{}'.format(N)+'e}'
return(FMT)

def Significant(x, *Error):
N = 0 if Error ==() else Sig_Digit(abs(Error[0]), 1)
m = 10**N
y = round(x*m)/m
return(y)

def Scientific(x, *N):
m = 10**Sig_Digit(x, *N)
y = round(x*m)/m
return(y)

def Scientific_Str(x, *N,):
FMT = Sci_Format(x, *N)
return(FMT.format(x))

def Significant_Str(x, *Error,):
FMT = Sig_Format(x, *Error)
return(FMT.format(x))
``````

``````X = [19.03345607, 12.075, 360.108321344, 4325.007605343]
Error = [1.245, 0.1245, 0.0563, 0.01245, 0.001563, 0.0004603]
for x in X:
for error in Error:
print(x,'+/-',error, end='   \t==> ')
print(' (',Significant_Str(x, error), '+/-', Scientific_Str(error),')')

``````

``````19.03345607 +/- 1.245       ==>  ( 19 +/-  1 )

19.03345607 +/- 0.1245      ==>  ( 19.0 +/- 0.1 )

19.03345607 +/- 0.0563      ==>  ( 19.03 +/- 0.06 )

19.03345607 +/- 0.01245     ==>  ( 19.03 +/- 0.01 )

19.03345607 +/- 0.001563    ==>  ( 19.033 +/- 0.002 )

19.03345607 +/- 0.0004603       ==>  ( 19.0335 +/- 0.0005 )

12.075 +/- 1.245    ==>  ( 12 +/-  1 )

12.075 +/- 0.1245       ==>  ( 12.1 +/- 0.1 )

12.075 +/- 0.0563       ==>  ( 12.07 +/- 0.06 )

12.075 +/- 0.01245      ==>  ( 12.07 +/- 0.01 )

12.075 +/- 0.001563     ==>  ( 12.075 +/- 0.002 )

12.075 +/- 0.0004603    ==>  ( 12.0750 +/- 0.0005 )

360.108321344 +/- 1.245     ==>  ( 360 +/-  1 )

360.108321344 +/- 0.1245    ==>  ( 360.1 +/- 0.1 )

360.108321344 +/- 0.0563    ==>  ( 360.11 +/- 0.06 )

360.108321344 +/- 0.01245       ==>  ( 360.11 +/- 0.01 )

360.108321344 +/- 0.001563      ==>  ( 360.108 +/- 0.002 )

360.108321344 +/- 0.0004603     ==>  ( 360.1083 +/- 0.0005 )

4325.007605343 +/- 1.245    ==>  ( 4325 +/-  1 )

4325.007605343 +/- 0.1245       ==>  ( 4325.0 +/- 0.1 )

4325.007605343 +/- 0.0563       ==>  ( 4325.01 +/- 0.06 )

4325.007605343 +/- 0.01245      ==>  ( 4325.01 +/- 0.01 )

4325.007605343 +/- 0.001563     ==>  ( 4325.008 +/- 0.002 )

4325.007605343 +/- 0.0004603    ==>  ( 4325.0076 +/- 0.0005 )
``````
1 四舍五入到有效数字

2 如何在Java中将数字四舍五入到6位有效数字？

3 如何在Python中将度量及其误差估计四舍五入为指定数量的有效数字？

4 四舍五入到有效数字，VB

2013-01-15 20:35:50 2 1192   rounding
6 为什么我的十进制四舍五入到 1 位有效数字？

7 在 numpy 中四舍五入到有效数字

2013-09-20 11:13:48 13 34367   numpy
8 四舍五入到任意数量的有效数字

9 四舍五入有效数字规则

2014-01-29 02:02:08 2 1789   vba/ excel
10 Postgresql 四舍五入到有效数字