打印浮点数与特定数目的零 [英] Printing floats with a specific number of zeros
问题描述
例如:
我知道如何控制小数位数,但是如何控制零的数目?
104.06250000 - > 104.0625
119.00000 - > 119.0
72.000000 - > 72.0
在python中定义格式的格式是从C继承而来的,我很确定你在C中没有所需的格式。
现在,在python 3中,您有格式特殊功能,您可以在其中进行自己的格式设置。删除python中的最后一个零非常简单:只需使用strip方法:
$ $ $ $ $ c $ a $ 1.23040000
print str( a).rstrip('0')
如果你想保留小数点后的0,这也不是很困难。
I know how to control the number of decimals, but how do I control the number of zeros specifically?
For example:
104.06250000 -> 104.0625
119.00000 -> 119.0
72.000000 -> 72.0
I don't think there is a pre-defined format for this - format in python are inherited from C, and I am pretty sure you don't have the desired format in C.
Now, in python 3, you have the format special function, where you can do your own formatting. Removing the last zeros in python is very easy: just use the strip method:
a = 1.23040000
print str(a).rstrip('0')
If you want to keep a 0 after the decimal point, that's not very difficult either.
这篇关于打印浮点数与特定数目的零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!