我有两个带有不同数字位的十进制数(如3.60和12.750), 我将在输出中具有相同的长度,如下面的(在这种情况下,它们都有7个字符):
3.60000和12.7500
答案 0 :(得分:1)
nums = [3.60, 12.750]
for num in nums:
s = "%.5f" % num # 5 because it's 7-2
s = s[:7] # Accounts for the '.'
print(s)
输出:
3.60000
12.7500
# Each has length 7.