[英]Length of a float without trailing zeros (python)
I want to print the length of a float number without trailing zeros using python.我想使用 python 打印一个浮点数的长度而不用尾随零。 examples:
例子:
0.001000 >>> I want to get length=5
0.000100 >>> I want to get length=6
0.010000 >>> I want to get length=4
any suggestions?有什么建议么?
Try with this:试试这个:
inp = '0.00100'
len(str(float(inp)))
Output: Output:
This gives the length as 5这给出了长度为 5
All the trailing zeroes will be removed.所有尾随零将被删除。
Converting a float to a str will automatically remove tailing zeros:将 float 转换为 str 将自动删除拖尾零:
numbers = [0.0010000, 0.00000000100, 0.010000]
for number in numbers:
number = '{0:.16f}'.format(number).rstrip("0")
print(f"Converted to String: {str(number)} - Length: {len(str(number))}")
Results:结果:
Converted to String: 0.001 - Length: 5
Converted to String: 0.000000001 - Length: 11
Converted to String: 0.01 - Length: 4
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.