英文文档:

round(number[, ndigits])
Return the floating point value number rounded to ndigits digits after the decimal point. If ndigits is omitted, it returns the nearest integer to its input. 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.
Note

The behavior of round() for floats can be surprising: for example, round(2.675, 2) gives 2.67 instead of the expected 2.68. This is not a bug: it’s a result of the fact that most decimal fractions can’t be represented exactly as a float. See Floating Point Arithmetic: Issues and Limitations for more information.

说明:

  1. round函数用于对浮点数进行四舍五入求值,具体保留几位小数,以传入的ndigits参数来控制。

>>> round(1.1314926,1)
1.1
>>> round(1.1314926,5)
1.13149

  2. ndigits参数为可选参数,当不传入时,即以默认保留0位小数进行取整,返回的是整数。

>>> round(1.1314926)
1

  3. ndigits参数传入0时,虽然与不传入ndigits参数一样保留0位小数进行四舍五入,但是返回的值是浮点型。

>>> round(1.1314926,0)
1.0

  4. ndigits参数小于0时,对整数部分进行四舍五入,ndigits参数控制了对浮点数的整数部分的后几位进行四舍五入,小数部分全部清0,返回类型是浮点数。如果传入的浮点数的整数部分位数小于ndigits参数绝对值,则返回0.0.

>>> round(11314.926,-1)
11310.0
>>> round(11314.926,-3)
11000.0
>>> round(11314.926,-4)
10000.0
>>> round(11314.926,-5)
0.0

  5. round四舍五入时是遵循靠近0原则,所以-0.5和0.5进行0位四舍五入,返回的都是0.

>>> round(0.5)
0
>>> round(-0.5)
0

  6. 对于浮点数求四舍五入有一个陷阱,有些四舍五入结果不像预期那样,比如round(2.675, 2) 的结果是2.67 而不是预期的 2.68,这不是bug,而是浮点数在存储的时候因为位数有限,实际存储的值和显示的值有一定误差。

>>> round(2.675, 2)
2.67

  7. 对整数也能进行round操作,返回值也是整形。

>>> round(134567)
134567
>>> round(134567,0)
134567
>>> round(134567,1)
134567
>>> round(134567,2)
134567
>>> round(134567,-2)
134600
>>> round(134567,-6)
0

Python内置函数(55)——round的更多相关文章

  1. Python内置函数(6)——round

    英文文档: round(number[, ndigits]) Return the floating point value number rounded to ndigits digits afte ...

  2. Python内置函数(55)——globals

    英文文档: globals() Return a dictionary representing the current global symbol table. This is always the ...

  3. python内置函数

    python内置函数 官方文档:点击 在这里我只列举一些常见的内置函数用法 1.abs()[求数字的绝对值] >>> abs(-13) 13 2.all() 判断所有集合元素都为真的 ...

  4. Python入门之 Python内置函数

    Python入门之 Python内置函数 函数就是以功能为导向,一个函数封装一个功能,那么Python将一些常用的功能(比如len)给我们封装成了一个一个的函数,供我们使用,他们不仅效率高(底层都是用 ...

  5. Python | 内置函数(BIF)

    Python内置函数 | V3.9.1 | 共计155个 还没学完, 还没记录完, 不知道自己能不能坚持记录下去 1.ArithmeticError 2.AssertionError 3.Attrib ...

  6. python 内置函数和函数装饰器

    python内置函数 1.数学相关 abs(x) 取x绝对值 divmode(x,y) 取x除以y的商和余数,常用做分页,返回商和余数组成一个元组 pow(x,y[,z]) 取x的y次方 ,等同于x ...

  7. Python 内置函数笔记

    其中有几个方法没怎么用过, 所以没整理到 Python内置函数 abs(a) 返回a的绝对值.该参数可以是整数或浮点数.如果参数是一个复数,则返回其大小 all(a) 如果元组.列表里面的所有元素都非 ...

  8. 【转】python 内置函数总结(大部分)

    [转]python 内置函数总结(大部分) python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为 ...

  9. python内置函数,匿名函数

    一.匿名函数 匿名函数:为了解决那些功能很简单的需求而设计的一句话函数 def calc(n): return n**n print(calc(10)) #换成匿名函数 calc = lambda n ...

随机推荐

  1. James Munkres Topology: Sec 22 Exer 3

    Exercise 22.3 Let \(\pi_1: \mathbb{R} \times \mathbb{R} \rightarrow \mathbb{R}\) be projection on th ...

  2. ssm简单搭建

    目录结构 1.web.xml配置文件 <?xml version="1.0" encoding="UTF-8"?><web-app xmlns ...

  3. 1.用代码演示String类中的以下方法的用法 (2018.08.09作业)

    public class Test_001 { public static void main(String[] args) { String a = "德玛西亚!"; Strin ...

  4. JDK解压版制作

    今天上  甲骨文 的官网,想下载一个 解压版的 jdk 1.8 结果发现: 没有解压版,只有exe版本.于是就搜索制作方法. 具体就是,双击exe版本,在最后关头要安装的时候, 重点来了! 跟他皮一下 ...

  5. 猎鱼达人_PC按键

    更新模式[强制] 更新版本[3.13] 更新链接[https://yunfei-1256035889.cos.ap-beijing.myqcloud.com/%E6%8C%89%E9%94%AE%E7 ...

  6. CF1013B And

    There is an array with n elements a1, a2, ..., an and the number x. In one operation you can select ...

  7. BandwagonHost

    https://kiwivm.64clouds.com/main-exec.php?mode=extras_shadowsocks https://kiwivm.64clouds.com/main-e ...

  8. iOS开发只简单动画实现

    1.动画旋转: [UIViewbeginAnimations:@"View Flip"context:nil]; //声明,@"XX"为标题, [UIViews ...

  9. REM——适合移动开发的自适应方案

    文章目录 1.什么是REM 2.REM和EM的区别 3.手机端方案的特点 4.使用JS动态调整REM 5.REM与其他单位同时存在 1.什么是REM 先来认识几个常见单位: px:像素,这个大家都知道 ...

  10. TimesTen中文乱码问题(其实是cmd.exe中文乱码)

    解决了导入数据的问题后,从导师那儿拷贝了一些样例数据,结果发现cmd.exe中文乱码(开始我以为是TimesTen无法显示中文).在网上搜了很多,因为我以为是TimesTen无法显示中文,所以都是搜的 ...