0

I want to convert string numbers on a list to float numbers and i can't do it, so i need some help.

num = '0.00003533'

print('{:f}'.format(float(num)))

formatting it without decimals, only returns a float of 0.000035, i need the entire string in a float.

print('{:8f}'.format(float(num)))

adding the exact decimal works, but the numbers in the list with decimals varies greatly, so i can't manually add it everytime, how could i automatically add the correct decimal number inside the format? something like '{':exactdecimalf'} exactdecinal being a variable.

i'm using a module that requires float, which is why i can't print it directly from the string format.

Dwayne
  • 39
  • 1
  • 5

3 Answers3

0

Maybe double precision will suit you.

from decimal import Decimal
print ('{:f}'.format(Decimal(num)))
Tajinder Singh
  • 1,361
  • 2
  • 14
  • 26
0

Use this

from decimal import Decimal
num = '0.00003533'
print(Decimal(num)) #0.00003533 

if you want to print as string

print ('{:f}'.format(Decimal(num)))
nishant
  • 2,526
  • 1
  • 13
  • 19
-1

You can split the string and take the length of the last part with

len(num.split(".")[1])

Then use that as the number of decimals.

mfp2001
  • 19
  • 1
  • 2