将浮点数舍入为x个小数? [英] Round float to x decimals?
本文介绍了将浮点数舍入为x个小数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有一种方法可以将python浮点数舍入为x个小数?例如:
Is there a way to round a python float to x decimals? For example:
>>> x = roundfloat(66.66666666666, 4)
66.6667
>>>x = roundfloat(1.29578293, 6)
1.295783
我找到了修整/截断(66.666666666-> 66.6666)的方法,但不是修整(66.666666666-> 66.6667)的方法.
I've found ways to trim/truncate them (66.666666666 --> 66.6666), but not round (66.666666666 --> 66.6667).
推荐答案
使用内置函数round()
:
In [23]: round(66.66666666666,4)
Out[23]: 66.6667
In [24]: round(1.29578293,6)
Out[24]: 1.295783
关于round()
的帮助:
round(number [,ndigits])->浮点数
round(number[, ndigits]) -> floating point number
将数字四舍五入到给定的精度(以十进制数字表示)(默认为0 数字).这总是返回一个浮点数.精度可能 是负面的.
Round a number to a given precision in decimal digits (default 0 digits). This always returns a floating point number. Precision may be negative.
这篇关于将浮点数舍入为x个小数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文