I found this which covers how to suppress scientific notation, but I'd like to go a step further and also work out the formatting string (i.e. the number of required decimal points to represent a number).
My idea so far is to assume a very high resolution (20 in this example) and reduce trailing zeroes as suggested in the accepted answer here. Is there perhaps a better way to achieve this?
>>> f = 0.00000008
>>> s = '{:.20f}'.format(f)
>>> s
'0.00000008000000000000'
>>> s.rstrip('0')
'0.00000008'
Note, I don't want scientific notation either (which you would get with a formatting string of {:g}).
The max number of significant digits in a float is 15 (the power is separate from this). So there's no point in accounting for more digits past that, as they wouldn't be right. Knowing what the power of ten for a given number will tell you how many zeros to pad out in the front.
If what you're formatting will never get above one, then the following will suffice:
from math import log10
def floatformat(f):
pwr = log10(f)
return '{:.0{}f}'.format(f, int(abs(pwr)) + 15).rstrip('0')
But if you're parsing any possible float value, you'll have to handle large numbers a bit differently to get trailing zeros and not random inaccurate digits.
def floatformat(f):
sigs = 15 # number of accurate digits that a float can contain
pwr = log10(f)
if pwr > sigs: # power above accurate digits
s = '{:.{}f}'.format(f / 10 ** int(pwr), sigs)
s = s.replace('.', '') # remove decimal point, no longer necessary here
s = s + '0' * (int(pwr) - sigs) # add in trailing zeros
elif 0 < pwr <= sigs: # power within accurate digits
s = '{:.{}f}'.format(f, sigs - int(pwr)).rstrip('0')
else: # power below accurate digits
s = '{:.0{}f}'.format(f, int(abs(pwr)) + sigs).rstrip('0')
if s[-1] == '.': s = s[:-1] # remove trailing decimal point if needed
return s
All this is doing is keeping the accurate digits, then shuffling them around to have the correct power without the scientific notation.
Examples:
>>> floatformat(0.00000008)
'0.00000008'
>>> floatformat(0.0000000000000000000000000000008)
'0.0000000000000000000000000000008'
>>> floatformat(0.00000000000000000000000000000080067)
'0.00000000000000000000000000000080067'
>>> floatformat(2.31451103e7)
'23145110.3'
>>> floatformat(2.31451103e3)
'2314.51103'
>>> 935.16087e203 == float(floatformat(935.16087e203)) # conversion check to see if power is handled correctly
True
>>> import sys
>>> floatformat(sys.float_info.max)
'179769313486231600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'
>>> floatformat(sys.float_info.min)
'0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022250738585072'
Related
I'm really curious about why the behaviour is so different when add this "f" to the end of the number which I want to display:
# CASE with f
float1 = 10.1444786
print(f"{float1:.4f}")
# print out: 10.1445
# CASE without f
print(f"{float1:.4}")
# print out: 10.14
Why only 2 characters are displayed in the second example?
The implied type specifier is g, as given in the documentation Thanks #Barmar for adding a comment with this info!
None: For float this is the same as 'g', except that when fixed-point notation is used to format the result, it always includes at least one digit past the decimal point. The precision used is as large as needed to represent the given value faithfully.
For Decimal, this is the same as either 'g' or 'G' depending on the value of context.capitals for the current decimal context.
The overall effect is to match the output of str() as altered by the other format modifiers.
An experiment:
for _ in range(10000):
r = random.random() * random.randint(1, 10)
assert f"{r:.6}" == f"{r:.6g}"
Works every time
From https://docs.python.org/3/library/string.html#formatstrings,
General format. For a given precision p >= 1, this rounds the number to p significant digits and then formats the result in either fixed-point format or in scientific notation, depending on its magnitude. A precision of 0 is treated as equivalent to a precision of 1.
So in your second example, you ask for 4 sigfigs, but in your first you ask for 4 digits of precision.
Assume I have a float:
x = 0.0005953829144211724
I have to round it after the decimal to:
x = 0.00059
Similarly, if
x = 0.00000046605219739046376
then the result should be
x = 0.00000046
Is there any inbuild function in python to do this?
You can use a nested format with Decimal. The first format does the rounding using the "g" specifier. The second one prints all the digits, without scientific notation, using the decimal value of the rounded string:
from decimal import Decimal
x = 0.0005953829144211724
print(f"{Decimal(f'{x:.2g}'):f}") # 0.0006
print(f"{Decimal(f'{x:.3g}'):f}") # 0.000595
print(f"{Decimal(f'{x:.4g}'):f}") # 0.0005954
x = 0.00000046605219739046376
print(f"{Decimal(f'{x:.2g}'):f}") # 0.00000047
print(f"{Decimal(f'{x:.3g}'):f}") # 0.000000466
print(f"{Decimal(f'{x:.4g}'):f}") # 0.0000004661
Note that this DOES round the value to the specified precision, contrary to your examples which truncate the mantissa instead of rounding it
As shown in the other answer, if you want a certain number of significant digits, you should format the number in scientific notation. If, however, you want those significant digits in the "normal" format, you might either convert that scientific notation back to float (thus "forgetting" all the "insignificant" digits) and then back to string and rstrip all excess zeros, or maybe use a regular expression:
>>> x = 0.00000046605219739046376
>>> f'{float(f"{x:.2g}"):.20f}'.rstrip("0")
'0.00000047'
>>> re.match(r"0\.0*[^0]{2}", f"{x:.20f}").group()
'0.00000046'
Note: i) The .20f here means "print in normal decimal format with 20 places after the decimal", where the 20 is kind of arbitrary. ii) The regex will not round but just trim the number.
You can do something close with the g specifier in an f-string:
x = 0.00000046605219739046376
print(f'{x:.2g}')
This will print the result in "scientific notation"
4.7e-07
Similarly:
x = 0.0005953829144211724
print(f'{x:.2g}')
results in
0.0006
since it rounds up.
I have some number 0.0000002345E^-60. I want to print the floating point value as it is.
What is the way to do it?
print %f truncates it to 6 digits. Also %n.nf gives fixed numbers. What is the way to print without truncation.
Like this?
>>> print('{:.100f}'.format(0.0000002345E-60))
0.0000000000000000000000000000000000000000000000000000000000000000002344999999999999860343602938602754
As you might notice from the output, it’s not really that clear how you want to do it. Due to the float representation you lose precision and can’t really represent the number precisely. As such it’s not really clear where you want the number to stop displaying.
Also note that the exponential representation is often used to more explicitly show the number of significant digits the number has.
You could also use decimal to not lose the precision due to binary float truncation:
>>> from decimal import Decimal
>>> d = Decimal('0.0000002345E-60')
>>> p = abs(d.as_tuple().exponent)
>>> print(('{:.%df}' % p).format(d))
0.0000000000000000000000000000000000000000000000000000000000000000002345
You can use decimal.Decimal:
>>> from decimal import Decimal
>>> str(Decimal(0.0000002345e-60))
'2.344999999999999860343602938602754401109865640550232148836753621775217856801120686600683401464097113374472942165409862789978024748827516129306833728589548440037314681709534891496105046826414763927459716796875E-67'
This is the actual value of float created by literal 0.0000002345e-60. Its value is a number representable as python float which is closest to actual 0.0000002345 * 10**-60.
float should be generally used for approximate calculations. If you want accurate results you should use something else, like mentioned Decimal.
If I understand, you want to print a float?
The problem is, you cannot print a float.
You can only print a string representation of a float. So, in short, you cannot print a float, that is your answer.
If you accept that you need to print a string representation of a float, and your question is how specify your preferred format for the string representations of your floats, then judging by the comments you have been very unclear in your question.
If you would like to print the string representations of your floats in exponent notation, then the format specification language allows this:
{:g} or {:G}, depending whether or not you want the E in the output to be capitalized). This gets around the default precision for e and E types, which leads to unwanted trailing 0s in the part before the exponent symbol.
Assuming your value is my_float, "{:G}".format(my_float) would print the output the way that the Python interpreter prints it. You could probably just print the number without any formatting and get the same exact result.
If your goal is to print the string representation of the float with its current precision, in non-exponentiated form, User poke describes a good way to do this by casting the float to a Decimal object.
If, for some reason, you do not want to do this, you can do something like is mentioned in this answer. However, you should set 'max_digits' to sys.float_info.max_10_exp, instead of 14 used in the answer. This requires you to import sys at some point prior in the code.
A full example of this would be:
import math
import sys
def precision_and_scale(x):
max_digits = sys.float_info.max_10_exp
int_part = int(abs(x))
magnitude = 1 if int_part == 0 else int(math.log10(int_part)) + 1
if magnitude >= max_digits:
return (magnitude, 0)
frac_part = abs(x) - int_part
multiplier = 10 ** (max_digits - magnitude)
frac_digits = multiplier + int(multiplier * frac_part + 0.5)
while frac_digits % 10 == 0:
frac_digits /= 10
scale = int(math.log10(frac_digits))
return (magnitude + scale, scale)
f = 0.0000002345E^-60
p, s = precision_and_scale(f)
print "{:.{p}f}".format(f, p=p)
But I think the method involving casting to Decimal is probably better, overall.
I would like to format my floats with a fixed amount of digits. Right now I'm doing the following
format="%6.6g"
print(format%0.00215165)
print(format%1.23260)
print(format%145.5655)
But this outputs
0.00215165
1.2326
145.565
I also tried format="%6.6f" but it doesn't really give what I want either...
0.002152
1.232600
145.565500
What would be a good way to format the numbers so that all of them have exactly width 6 (and no spaces) like so ?
0.002152
1.232600
145.5655
This is complicated because you want the precision (number of decimals) to depend on the available space, while the general thrust of floating-point formatting is to make the number of significant digits depend on the available space. To do what you want you need a function that computes the desired number of decimals from the log of the number. There isn't, so far as I know, a built-in function that will do this for you.
def decimals(v):
return max(0, min(6,6-int(math.log10(abs(v))))) if v else 6
This simply takes the log of number and truncates it to int. So 10-99 -> 1, 100-999 -> 2 etc. You then use that
result to work out the precision to which the number needs to be formatted. In practice the
function is more complex because of the corner cases: what to do with negative numbers, numbers that underflow, etc.
For simplicity I've deliberately left your figure of 6 decimals hard-coded 3 times in the function.
Then formatting isn't so hard:
>>> v = 0.00215165
>>> "{0:.{1}f}".format(v, decimals(v))
'0.002152'
>>> v2 = 1.23260
>>> "{0:.{1}f}".format(v2, decimals(v2))
'1.232600'
>>> v3 = 145.5655
>>> "{0:.{1}f}".format(v3, decimals(v3))
'145.5655'
>>> vz = 0e0 # behaviour with zero
>>> "{0:.{1}f}".format(vz, decimals(vz))
'0.000000'
>>> vu = 1e-10 # behaviour with underflow
>>> "{0:.{1}f}".format(vu, decimals(vu))
'0.000000'
>>> vo = 1234567 # behaviour when nearly out of space
>>> "{0:.{1}f}".format(vo, decimals(vo))
'1234567'
>>> voo = 12345678 # behaviour when all out of space
>>> "{0:.{1}f}".format(voo, decimals(voo))
'12345678'
You can use %-notation for this instead of a call to format but it is not very obvious or intuitive:
>>> "%.*f" % (decimals(v), v)
'0.002152'
You don't say what you want done with negative numbers. What this approach does is to take an extra
character to display the minus sign. If you don't want that then you need to reduce the number of
decimals for negative numbers.
For python 3 I want to convert a float to a string, with possibly different length (i.e. number of digits) but with full precision.
Also I need to have a decimal point in any case:
1 -> '1.'
1/10 -> '0.1000000000000000055511151231257827021181583404541015625'
currently my code is this:
from decimal import Decimal
def formatMostSignificantDigits(x):
out = str(Decimal(x))
if out.find('.') < 0:
out += '.'
return out
can this be done more elegantly? (e notation would be possible, too)
Use Pythons string formatting functions:
>>> x = 1.0; '{:.55f}'.format(x)
'1.0000000000000000000000000000000000000000000000000000000'
>>> x = 1/10; '{:.55f}'.format(x)
'0.1000000000000000055511151231257827021181583404541015625'
If you want to be able to feed it integers (such as 1) as well, use '{:.55f}'.format(float(x)).
If you want to strip any trailing zeroes, use '{:.55f}'.format(x).rstrip('0').
Note that 55 decimals after the point is way overkill (but it's what you showed in your question); 16 digits should suffice to express the full precision of double-precision IEEE 754 floats (20 digits for the 80-bit extended-precision you might encounter).
why do you use Decimal, you can just use:
x = 0.1
s = str(x)
print(s) # this prints '0.1'
but if you use Decimal for something else than instead of this:
out = str(Decimal(x))
if out.find('.') < 0:
out += '.'
return out
you can just use:
return Decimal(x).__str__()
Edit 1:
also good module for float precision is bigfloat:
from bigfloat import BigFloat
x = 0.1
print(BigFloat(x, precision(300)).__str__())
# thsi will print'0.10000000000000000555111512312578270211815834045410156250000000000000000000000000000000000000'
# but if you use this:
print(BigFloat(x.__str__(), precision(300)).__str__())
# it can be precise as much as you want
print(BigFloat(x.__str__(), precision(100000)).__str__()) # try this