Python - K 长度小数位
给定一个十进制数,将其数字扩展到 K 长度。
Input : num = 76.8, K = 5
Output : 76.80000
Explanation : Length of decimal places is 5.
Input : num = 76.8, K = 6
Output : 76.800000
Explanation : Length of decimal places is 6.
方法:使用格式()
这是可以完成此任务的方式。在此,我们利用多种格式兼容性来执行获取适当小数位长度的任务。
Python3
# Python3 code to demonstrate working of
# K length decimal Places
# Using format()
# initializing number
num = 76.8
# printing original number
print("The original number is : " + str(num))
# initializing K
K = 7
# using format to solve this problem
res = "{{:.{}f}}".format(K).format(num)
# printing result
print("The resultant number : " + str(res))
输出
The original number is : 76.8
The resultant number : 76.8000000