没有尾随零的浮点数的长度 (python)
Length of a float without trailing zeros (python)
我想使用 python 打印不带尾随零的浮点数的长度。
示例:
0.001000 >>> I want to get length=5
0.000100 >>> I want to get length=6
0.010000 >>> I want to get length=4
有什么建议吗?
试试这个:
inp = '0.00100'
len(str(float(inp)))
输出:
长度为 5
将删除所有尾随零。
将浮点数转换为字符串将自动删除尾随零:
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))}")
结果:
Converted to String: 0.001 - Length: 5
Converted to String: 0.000000001 - Length: 11
Converted to String: 0.01 - Length: 4
我想使用 python 打印不带尾随零的浮点数的长度。 示例:
0.001000 >>> I want to get length=5
0.000100 >>> I want to get length=6
0.010000 >>> I want to get length=4
有什么建议吗?
试试这个:
inp = '0.00100'
len(str(float(inp)))
输出:
长度为 5
将删除所有尾随零。
将浮点数转换为字符串将自动删除尾随零:
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))}")
结果:
Converted to String: 0.001 - Length: 5
Converted to String: 0.000000001 - Length: 11
Converted to String: 0.01 - Length: 4