0

I wouldlike to get 8 digit after comma but I tried everything, no one solution actually resolve my problem:

tab_freq=(result[1]/1e6)
value_freq=str(tab_freq)[1:-1]
print value_freq

I tried this :

 value_freq2=format(value_freq,".8f")

and I get always the same error :

Unknown format code 'f' for object of type 'str'

Thanks for your help !

azzerty2017
  • 43
  • 1
  • 1
  • 9

1 Answers1

3

".8f" will work on numbers, not strings.

Try converting it to a float first:

value_freq2=format(float(value_freq),".8f")


Edit:

In response to your comments below, you first need to split all the values in value_freq2 and apply the format to each one, then rejoin them.

value_freq = "100. 100.98 105.344444 104."
value_list = [format(float(v), ".8f") for v in value_freq.split()]
value_freq2 = "\n".join(value_list)
  • I tried but I have another error :`invalid literal for float():` 99.99999949 100. 100.254..... this function do not add 8 zero after comma ? – azzerty2017 Jul 06 '17 at 21:52
  • It looks like you're passing a list of values. You'll need to split it up with `str.split()` first. – Nathaniel Rivera Saul Jul 06 '17 at 21:54
  • With my script I send data by bluetooth with that : client_sock.send('\n'.join(value_freq2.split())) to ordonate my list of values when some value don't have the same lenght like : 105.and 104.99999949 for example. I want the same lenght like 105.00000000 – azzerty2017 Jul 06 '17 at 21:58
  • Okay, makes sense. You need to apply the format for each value in value_freq2.split() and then join that list. I'll edit the solution to address this. – Nathaniel Rivera Saul Jul 06 '17 at 22:03
  • so if I understand, `client_sock.send('\n'.join(format(value_freq2.split(), ".8f"))` ? – azzerty2017 Jul 06 '17 at 22:09