在Python中,在这种特殊情况下,如何对数字进行汇总?
我想弄清楚怎么搜捕。 Exmaple: 我怎样才能把这件事综合起来? 谢谢 编辑: 但我想:在Python中,在这种特殊情况下,如何对数字进行汇总?,python,Python,我想弄清楚怎么搜捕。 Exmaple: 我怎样才能把这件事综合起来? 谢谢 编辑: 但我想: >>>3.11 #as 3.1049-> 3.105->3.11 给你,伙计,阅读起来很简单,不需要任何新的库: num = 3.1049 print(%.2f" % num) 这将为您提供以下结果: >>> num = 3.1049 >>> print(%.2f" % num) >>> 3.10 >
>>>3.11 #as 3.1049-> 3.105->3.11
给你,伙计,阅读起来很简单,不需要任何新的库:
num = 3.1049
print(%.2f" % num)
这将为您提供以下结果:
>>> num = 3.1049
>>> print(%.2f" % num)
>>> 3.10
>>> num = 3.1051
>>> print(%.2f" % num)
>>> 3.11
因此,我们可以从中创建一个方法,如下所示:
>>> def round_hundredths(num):
return "%.2f" % num
>>> print(round_hundredths(3.1049))
哪些产出:
>>> 3.10
希望你明白这里发生了什么 我认为数学模块可能适合你的情况
import math
num = 3.1049
print(math.ceil(num*100)/100)
OP想集中注意力。可能是一个普遍的解决办法。Gumboy的解决方案有效。这是一个普遍的解决方案
def mround(x, n):
x_str = str(x).split('.')
for i in range(len(x_str[1]), n, -1):
s = 5 * 10 ** -i
if xs[1][-i + 1] == '0':
x = round(x, i - 1)
else:
x = round(x + s, i - 1)
return x
用法:
>>> mround(3.1049, 2)
3.11
>>> mround(3.1049, 1)
3.1
它使用一个循环,每个小数位四舍五入。它不是从9取整,而是从4取整。。。我想从9点算起。你说从9点算起是什么意思?9是小数点后的第四位,因此四舍五入将是3位小数,即3.105。到目前为止,您有什么代码?这不是一个代码编写服务。我的意思是如果我把9加起来,那么它是3.105。我希望这个数字是3.11!请帮帮我,伙计们。。。哈哈,为什么3.1049,1给出3.2@Ekhumaro固定
def mround(x, n):
x_str = str(x).split('.')
for i in range(len(x_str[1]), n, -1):
s = 5 * 10 ** -i
if xs[1][-i + 1] == '0':
x = round(x, i - 1)
else:
x = round(x + s, i - 1)
return x
>>> mround(3.1049, 2)
3.11
>>> mround(3.1049, 1)
3.1