How to Suppress Scientific Notation When Printing Float Values

How to suppress scientific notation when printing float values?

'%f' % (x/y)

but you need to manage precision yourself. e.g.,

'%f' % (1/10**8)

will display zeros only.

details are in the docs

Or for Python 3 the equivalent old formatting or the newer style formatting

how to prevent float variables displaying as scientific notation when printing

This is the scientific notation of float number. So, if you want to format then you should use number_format() function.
Below example the second parameter will tell at what precision do you need.
So, as per your example you should use 14.

Try this:

$var = number_format((float)-0.00000025478625, 14);
print($var);

Suppress Scientific Notation in a list

Try this, for more compressed values try '.2f' or what ever decimal points you want.

x = []
for i in range(0,50):
x.append(s.poisson.pmf(i, 10))
x[i] = format(x[i], 'f')
print(x)
plt.plot(x)

This is the output

How to avoid printing scientific notation in python without adding extra digits?

It seems to me a little hacky, but you can use str.rstrip("0") to get rid of trailing zeros:

>>> "{:f}".format(10**-6).rstrip("0")
'0.000001'
>>> "{:f}".format(10**-3).rstrip("0")
'0.001'

Edit: As said in comments, there is a better way for this:

>>> format(1e-6, 'f').rstrip('0')
'0.000001'
>>> format(1e-3, 'f').rstrip('0')
'0.001'

Preventing Scientific Notation in Arrays

Is this your anwser:

import decimal

l = [0.0000001 , 0.000001 , 0.00000001]

for i in l:
d = decimal.Decimal(str(i))
print(d.as_tuple())

e = abs(d.as_tuple().exponent)
print(f'{i:.{e}f}')

output:

DecimalTuple(sign=0, digits=(1,), exponent=-7)
0.0000001
DecimalTuple(sign=0, digits=(1,), exponent=-6)
0.000001
DecimalTuple(sign=0, digits=(1,), exponent=-8)
0.00000001


Related Topics



Leave a reply



Submit