0

I'm looking for a way to suppress scientific Notation on arrays that output floats of different lengths. All of the guides I've looked at show you how to suppress scientific notation for a singular value when you already know how many decimal places it is with code such as:

output=f"{num:8f}"

However is there a way to do this when iterating through a list where some values may be 0.0000001 (7 decimal places) some may be 0.000001 (6 decimal places), some may be 0.00000001 (8 decimal places), etc.?

All I want is a function that when iterating through a list and outputting values with large numbers of decimal places, it always just output the true value of the float, not its scientific notation, regardless of its number of decimal places

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Ben
  • 17
  • 5

2 Answers2

1

from: Easy way of finding decimal places Please see the post for issue regarding floating point accuracies. Since for example

1.1 - 1

has output

 0.10000000000000009

"the number of decimal places" is not a property a floating point number has, because of the way they are stored and handled internally. quote from one answer of the above link

It seems that one possible way to do the desired "printing effect" is:

from decimal import Decimal

for i in [5.5624, 6.454257, 0.0000001, 0.00000000000001]:
    e = abs(Decimal(str(i)).as_tuple().exponent)
    print(f'{i:.{e}f}')

output:

In [21]: for i in [5.5624, 6.454257, 0.0000001, 0.00000000000001]:
    ...:     e = abs(Decimal(str(i)).as_tuple().exponent)
    ...:     print(f'{i:.{e}f}')
    ...: 
    ...: 
    ...: 
    ...: 
    ...: 
5.5624
6.454257
0.0000001
0.00000000000001
Alfred Tso
  • 51
  • 4
0

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
I'mahdi
  • 23,382
  • 5
  • 22
  • 30
  • I wouldn't want the trailing zeroes after the 1 as that will mess with the code that's all. I would just want 0.000001000 to be 0.00001 which is why i'm looking for an option that omits the need to define a number of decimal places to float to if that makes sense – Ben Aug 09 '21 at 11:03