简体   繁体   English

在Python中逗号后得到8位数字

[英]get 8 digit after comma in python

I wouldlike to get 8 digit after comma but I tried everything, no one solution actually resolve my problem: 我想在逗号后得到8位数字,但我尝试了所有方法,没有一个解决方案可以实际解决我的问题:

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 ! 谢谢你的帮助 !

".8f" will work on numbers, not strings. ".8f"将适用于数字,而不适用于字符串。

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_freq2中的所有值,并将格式应用于每个值,然后重新加入它们。

 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) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM