`
田梦桦
  • 浏览: 18372 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

python中的函数round

阅读更多
     今天在比较python3.2和Python2.7的区别的时候,发现了一个小插曲。代码如下。

Python 2.7.2 (default, Jun 12 2011, 14:24:46) [MSC v.1500 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> round(1.0/2)
1.0
>>> round(3/2)
1.0
>>> round(3.0/2)
2.0
>>>


Python 3.2.2 (default, Sep  4 2011, 09:07:29) [MSC v.1500 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> round(1.0/2)
0
>>> round(3/2)
2
>>> round(3.0/2)
2
>>>

其中我就一点想不明白,为什么在python3.2中round(1.0/2)的值为0.我想python3这样写肯定有它的理由,不会是一个小bug,我就想知道这个理由。

论坛讨论的结果如下:
“For the built-in types supporting round(), values are rounded to the closest multiple of 10 to the power minus n; 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 x. ”
所以两端差相等时,会round成偶数那端
>>> round(0.5)
0
>>> round(2.5)
2
>>> round(1.5)
2


分享到:
评论

相关推荐

    python round函数用法

    python round函数用法

    python中round函数如何使用

    在本篇文章里小编给大家整理了关于python的round函数用法总结内容,需要的朋友们可以学习下。

    python中round函数保留两位小数的方法

    那在我们的python中如何四舍五入?如何保留小数?用round函数就可以。 1、round函数 python的内置函数,用于数字的四舍五入。 2、round 负数 四舍五入是围绕着0来计算的 示例 round(0.5) # 1.0 round(-0.5) #-1.0...

    hcldirgit#studygit#13. Python round函数1

    Python round()函数描述round() 方法返回浮点数x的四舍五入值。语法以下是 round() 方法的语法:参数x -- 数值表达式。返回值返回浮

    说说 Python 的 round 函数

    round( number ) 函数会返回浮点数 number 的四舍五入值。 具体定义为 round(number[,digits]): 如果 digits>0 ,四舍五入到指定的小数位; 如果 digits=0 ,四舍五入到最接近的整数; 如果 digits<0 ,则在...

    Python学习教程之常用的内置函数大全

    在Python中,python给我们提供了很多已经定义好的函数,这里列出常用的内置函数,分享出来供大家参考学习,下面话不多说,来一起看看详细的介绍吧。 一、数学函数 abs() 求数值的绝对值 min()列表的最下值 max()...

    马哥python课堂笔记-马哥教育PYTHON相关基础笔记.pdf

    6 数字的处理函数 math.ceil #向上取整(天花板) math.floor (i2nt)#向下取整, round() # 四舍六⼊ 五取偶 (2.5=2,3.5=4) 7 元组 y = (1,)逗号代表元组 命名元组namedtuple() from collections import namedtuple ...

    使用Python三角函数公式计算三角形的夹角案例

    题目内容: 对于三角形,三边长分别为a, b, c,给定a和b之间的夹角C,则有:。编写程序,使得输入三角形的边a, b, c,可求得夹角C(角度值)。 输入格式: 三条边a、b、c的长度值,每个值...print round(math.acos((a*a+b

    资料Python完全新手教程复习知识点试卷试题.doc

    资料Python完全新手教程复习知识点试卷试题 一、选择题 1.在python中,下列属于字符串常量的是( ) A."python" B.3.14 C.cnt D.2018 2.下列python表达式结果为5...在Python中,表示跳出循环的函数是( ) A.con

    Python的math模块中的常用数学函数整理

    在数学之中,除了加减乘除四则运算之外——这是小学数学——还有其它更多的运算,比如乘方、开方、对数运算等等,要实现这些运算,需要用到 Python 中的一个模块:Math 模块(module)是 Python 中非常重要的东西,你...

    python函数及一些小问题整理

    目录易错函数sorted()函数round()函数NB的eval()函数strip()函数map()&reduce()遇到的小问题复数运算进制转换cmd中使用python交互式format格式化输入输出bollean index 易错函数 sorted()函数 sorted(对象,...

    python笔记1.hello,python.txt

    python简单笔记,编码情况、保留小数、字符串格式化、round函数、re正则匹配模块

    python3 小数位的四舍五入(用两种方法解决round 遇5不进)

    菜鸟教程中介绍到,round() 函数作用就是,返回浮点数x的四舍五入值。 > round( x [, n] ) 参数x,n均为数值表达式,返回值为x的四舍五入值。n为保留的小数位数,不加n则只保留x四舍五入后的整数部分。 >>> round...

    初中信息技术第一课用python编程精品练习.doc

    以下Python中变量的命名正确的是( ) A.1a=4 B.print=5 C._A=2 D.a+b=3 2.Python输入函数为( )。 A.time() B.round() C.input( ) D.print() 3.在Python中print(-14//4)的运行结果是( )。 A.-4 B.-...

    python实现我们通常所了解的四舍五入,大于或等于5入,解决round()函数不完全四舍五入的问题

    python实现我们通常所了解的四舍五入,大于或等于5入,解决round()函数不完全四舍五入的问题;使用方法 把文件放在python模块文件夹里 使用时: import sswr sswr.sswr(num,ws) #unm为要四舍五入的数,ws为保留小数...

    试题青少年python三级真题21-05word练习.doc

    机器语言 3.Python输入函数为( )。 A.time() B.round() C.input( ) D.print() 4.下列python表达式结果最小的是( ) A.2**3//3+8%2*3 B.5**2%3+7%2**2 C.1314//100%10 D.int("1"+"5")//3 5.下列...

    Python3 菜鸟查询手册

    07.12 数学函数 round() 函数.png 07.13 数学函数 sqrt() 函数.png 07.14 随机数函数 choice() 函数.png 07.15 随机数函数 randrange() 函数.png 07.16 随机数函数 random() 函数.png 07.17 随机数函数 seed()...

    python小白之有关函数的那些事

    python小白之第二节-有关函数的那些事 Reference: Kaggle Notebook Functions and Getting Help! Click me! 上一节有提到过print()函数,但是...help()的函数可以说是python函数中最重要的一位成员了。只需要记住hel

Global site tag (gtag.js) - Google Analytics