2024-10-04 09:27:34 发布
网友
在Python中,round(0.625)返回0.62。为什么会这样? 以下代码仅适用于正数,有什么更好的选择
from maths import floor def round2Dp(n, decimals=0): multiplier = 10 ** decimals return floor(n*multiplier+0.5) / multiplier
GeeksForGooks.com说,“当第二个参数存在时,它返回: 当第(ndigit+1)个数字>;=5时,最后一个四舍五入的十进制数字将增加1,否则保持不变。”
有人试图拒绝这个答案,说他很确定网站上没有这么说。这是从这个页面直接剪切粘贴的:https://www.geeksforgeeks.org/round-function-python/
有关数字类型here,请参阅文档。具体而言:
round(x[, n]): x rounded to n digits, rounding half to even. If n is omitted, it defaults to 0.
这基本上意味着一半(0.5)总是四舍五入到最接近的偶数数
GeeksForGooks.com说,“当第二个参数存在时,它返回: 当第(ndigit+1)个数字>;=5时,最后一个四舍五入的十进制数字将增加1,否则保持不变。”
有人试图拒绝这个答案,说他很确定网站上没有这么说。这是从这个页面直接剪切粘贴的:https://www.geeksforgeeks.org/round-function-python/
有关数字类型here,请参阅文档。具体而言:
这基本上意味着一半(0.5)总是四舍五入到最接近的偶数数
相关问题 更多 >
编程相关推荐