[英]How to round float to certain amount of decimal places Python
I have a randomly generated float from the random library, and I want to shorten it to any number of places, based on some other number.我从随机库中随机生成了一个浮点数,我想根据其他一些数字将其缩短到任意数量的位置。
Example:例子:
input = 4.06098152输入 = 4.06098152
processing the number, rounding to thousandths place处理数字,四舍五入到千分位
output = 4.061输出 = 4.061
How would I do this?我该怎么做?
You can use the built in round
function: The second argument in the paranthesis gives the amount of digits to round off您可以使用内置的
round
函数:括号中的第二个参数给出要四舍五入的位数
x=input("Enter a decimal number: ")
print(round(float(x),3))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.