Round() function in Python follows the half to even rounding strategy. In this strategy, the number is rounded off to its nearest even integer. For example, if we need to round off 7.5, it will be rounded off to its nearest even integer that is 8.
The “g” format specifier is a general format that can be used to indicate a precision, or to indicate significant digits. To print a number with a specific number of significant digits we do this: print '{0:1.3g}'. format(1./3.)
5 is round up for positive values and round down for negative values. For instance, both round(0.5) and round(-0.5) return 0 , while round(1.5) gives 2 and round(-1.5) gives -2 . This Python behaviour is a bit different from how rounding usually goes.
You can use negative numbers to round integers:
>>> round(1234, -3)
1000.0
Thus if you need only most significant digit:
>>> 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
You'll probably have to take care of turning float to integer if it's bigger than 1.
%g in string formatting will format a float rounded to some number of significant figures. It will sometimes use 'e' scientific notation, so convert the rounded string back to a float then through %s string formatting.
>>> '%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'
If you want to have other than 1 significant decimal (otherwise the same as Evgeny):
>>> 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)))
This solution is different from all of the others because:
For an arbitrary number n
of significant figures, you can use:
print('{:g}'.format(float('{:.{p}g}'.format(i, p=n))))
Test:
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']
Note: with this solution, it is not possible to adapt the number of significant figures dynamically from the input because there is no standard way to distinguish numbers with different numbers of trailing zeros (3.14 == 3.1400
). If you need to do so, then non-standard functions like the ones provided in the to-precision package are needed.
I have created the package to-precision that does what you want. It allows you to give your numbers more or less significant figures.
It also outputs standard, scientific, and engineering notation with a specified number of significant figures.
In the accepted answer there is the line
>>> round_to_1(1234243)
1000000.0
That actually specifies 8 sig figs. For the number 1234243 my library only displays one significant figure:
>>> from to_precision import to_precision
>>> to_precision(1234243, 1, 'std')
'1000000'
>>> to_precision(1234243, 1, 'sci')
'1e6'
>>> to_precision(1234243, 1, 'eng')
'1e6'
It will also round the last significant figure and can automatically choose what notation to use if a notation isn't specified:
>>> to_precision(599, 2)
'600'
>>> to_precision(1164, 2)
'1.2e3'
To directly answer the question, here's my version using naming from the R function:
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)
My main reason for posting this answer are the comments complaining that "0.075" rounds to 0.07 rather than 0.08. This is due, as pointed out by "Novice C", to a combination of floating point arithmetic having both finite precision and a base-2 representation. The nearest number to 0.075 that can actually be represented is slightly smaller, hence rounding comes out differently than you might naively expect.
Also note that this applies to any use of non-decimal floating point arithmetic, e.g. C and Java both have the same issue.
To show in more detail, we ask Python to format the number in "hex" format:
0.075.hex()
which gives us: 0x1.3333333333333p-4
. The reason for doing this is that the normal decimal representation often involves rounding and hence is not how the computer actually "sees" the number. If you're not used to this format, a couple of useful references are the Python docs and the C standard.
To show how these numbers work a bit, we can get back to our starting point by doing:
0x13333333333333 / 16**13 * 2**-4
which should should print out 0.075
. 16**13
is because there are 13 hexadecimal digits after the decimal point, and 2**-4
is because hex exponents are base-2.
Now we have some idea of how floats are represented we can use the decimal
module to give us some more precision, showing us what's going on:
from decimal import Decimal
Decimal(0x13333333333333) / 16**13 / 2**4
giving: 0.07499999999999999722444243844
and hopefully explaining why round(0.075, 2)
evaluates to 0.07
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With