鱼C论坛

 找回密码
 立即注册
查看: 5566|回复: 6

round(0.5)为何会等于0?

[复制链接]
发表于 2016-7-10 16:39:13 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
round()是四舍五入函数,round(1.5)等于2,但round(0.5)为啥等于0?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-7-10 16:57:28 | 显示全部楼层
在2中round(0.5)是1…
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-7-10 17:48:49 | 显示全部楼层
round(number[, ndigits])
Return the floating point value number rounded to ndigits digits after the decimal point. If ndigits is omitted, it defaults to zero. Delegates to number.__round__(ndigits).

For the built-in types supporting round(), values are rounded to the closest multiple of 10 to the power minus ndigits; if two multiples are equally close, rounding is done toward the even choice (so, for example, both round(0.5) and round(-0.5) are 0, and round(1.5) is 2). The return value is an integer if called with one argument, otherwise of the same type as number.

---
round的返回值会选择最接近10的倍数的那个(也就是四舍五入);如果两边的数几率相等,那么会选择是偶数的那一个。(举个例子,round(0.5)和round(-0.5)都是0,round(1.5)就会是2),如果只传入一个参数,那么函数会返回一个整数,否则会返回指定参数同类的数。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-7-10 18:39:00 | 显示全部楼层
残风 发表于 2016-7-10 16:57
在2中round(0.5)是1…

是在python3中
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-7-10 19:30:51 | 显示全部楼层
wei_Y 发表于 2016-7-10 17:48
round(number[, ndigits])
Return the floating point value number rounded to ndigits digits after th ...

果然,学习了,python3中这样设置是有什么意图吗?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-7-10 19:44:46 | 显示全部楼层
qqq111 发表于 2016-7-10 19:30
果然,学习了,python3中这样设置是有什么意图吗?

这种方法叫“四舍六入五成双”,是为了避免在大量数据统计时舍入误差引起整体的偏移。

评分

参与人数 1荣誉 +3 鱼币 +3 贡献 +3 收起 理由
小火木 + 3 + 3 + 3 支持楼主!

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-7-11 20:23:02 | 显示全部楼层
冬雪雪冬 发表于 2016-7-10 19:44
这种方法叫“四舍六入五成双”,是为了避免在大量数据统计时舍入误差引起整体的偏移。

大数据统计常常用到,明白了,谢谢~~~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 15:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表