python round

Python round() function is used to perform rounding operation on numbers.

Python round()函数用于对数字执行舍入运算。

python round() (Python round())

Python round() function syntax is:

Python round()函数语法为:

round(number[, ndigits])

The number is rounded to ndigits precision after the decimal point.

数字在小数点后四舍五入为n位精度。

If ndigit is not provided or is None, then nearest integer is returned.

如果未提供ndigit或为None,则返回最接近的整数。

While rounding the input number to an integer, if both round up and round down values are equally close then even number is returned. For example, 10.5 will be rounded to 10 whereas 11.5 will be rounded to 12.

将输入数字四舍五入为整数时,如果四舍五入值和四舍五入值均相等,则返回偶数。 例如,将10.5舍入为10,而将11.5舍入为12。

Any integer value is valid for ndigits (positive, zero, or negative).

任何整数值都对n位数字有效(正数,零或负数)。

Python round()函数示例 (Python round() function examples)

Let’s look at some example of round() function.

让我们看一下round()函数的一些示例。

round() to integer

round()转换为整数

print(round(10, 2))print(round(10.2))
print(round(10.8))
print(round(11.5))

Output:

输出:

10
10
11
12

round() to even side

将round()整齐

# if both side of rounding is same, even is returned
print(round(10.5))
print(round(12.5))

Output:

输出:

10
12

round() with ndigit as None

round(),ndigit为无

print(round(1.5))
# OR
print(round(1.5, None))

Output:

输出:

2
2

round() with negative ndigit

负ndigit的round()

print(round(100, 0))
print(round(100.1234, -4))
print(round(100.1234, -5))

Output:

输出:

100
100.0
0.0

Python圆形浮点数 (Python round float)

When rounding is applied on floating point numbers, the result can be sometimes surprising. It’s because the numbers are stored in binary format and mostly decimal fractions cannot be represented exactly as binary fractions.

将四舍五入到浮点数上时 ,结果有时会令人惊讶。 这是因为数字以二进制格式存储,并且大多数十进制小数不能完全表示为二进制小数。

Python does the approximation and presents us the rounded value, because of this floating point arithmetic can sometimes result in surprising values.

Python会进行近似并为我们提供四舍五入的值,因为这种浮点算法有时会产生令人惊讶的值。

For example:

例如:

>>>.1 + .1 == .2
True
>>>.1 + .1 + .1 == .3
False
>>>.1 + .1 + .1 + .1 == .4
True

Let’s see some examples of round() function with floats.

让我们看一些带有float的round()函数的示例。

print(round(2.675, 2))print(round(1.2356, 2))
print(round(-1.2356, 2))

Output:

输出:

2.67
1.24
-1.24

Notice that first float rounding seems wrong. Ideally, it should be rounded to 2.68.

请注意,第一次浮点舍入似乎是错误的。 理想情况下,它应四舍五入为2.68。

This is the limitation of arithmetic operations with floats, we shouldn’t rely on conditional logic when dealing with floating point numbers.

这是浮点数算术运算的局限性,在处理浮点数时我们不应该依赖条件逻辑。

round()与自定义对象 (round() with custom object)

We can use round() function with a custom object too if they implement __round__() function. Let’s look at an example.

如果它们实现__round __()函数,我们也可以将其与自定义对象一起使用。 让我们来看一个例子。

class Data:id = 0def __init__(self, i):self.id = idef __round__(self, n):return round(self.id, n)d = Data(10.5234)
print(round(d, 2))
print(round(d, 1))

Output:

输出:

10.52
10.5
GitHub Repository.GitHub存储库中检出完整的python脚本和更多Python示例。

Reference: Official Documentation

参考: 官方文档

翻译自: https://www.journaldev.com/23118/python-round

python round

python round_python round()相关推荐

  1. floor()函数与round()函数

    floor函数 floor函数取整,保留整数部分,舍弃小数部分,当时负数部分时,向远离0的方向取值 例如: math.floor(1.5) = 1.0 math.floor(-1.5) = -2.0 ...

  2. Python中的“ @”(@)符号有什么作用?

    我正在看一些使用@符号的Python代码,但我不知道它的作用. 我也不知道要搜索什么,因为搜索Python文档时会出现,或者当包含@符号时Google不会返回相关结果. #1楼 此代码段: def d ...

  3. 如何使用Python numpy.where()方法

    In Python, we can use the numpy.where() function to select elements from a numpy array, based on a c ...

  4. Python字符串title()

    Python String title() Python字符串title() Python String title() function returns a title cased version ...

  5. Python字符串isdecimal()

    Python String isdecimal() function returns True if all the characters in the string are decimal char ...

  6. Python字符串isalnum()

    Python string isalnum() function returns True if it's made of alphanumeric characters only. A charac ...

  7. Python字符串count()

    Python String count() function returns the number of occurrences of a substring in the given string. ...

  8. Python字符串splitlines()

    Python String splitlines() Python字符串splitlines() Python String splitlines() function returns the lis ...

  9. Python字符串join()方法

    Python string join() method creates a string from an iterable. It joins all the iterable elements wi ...

最新文章

  1. Go 学习笔记(2)— 安装目录、工作区、源码文件和标准命令
  2. 大规模神经网络的训练优化入门
  3. 滨州学院计算机自荐考试题型,滨州学院2004—2005学年第一学期期末考试计算机科学教育专业02级《数据库原理》试题及答案(3份,另附习题集)...
  4. DataTable排序,检索,合并,筛选
  5. 使用vue制作富文本框
  6. mysql题目_MySQL练习题
  7. java字符串笔试题_五道Java常见笔试题及答案汇总
  8. Office2010试用
  9. css3书页翻转,CSS3实现3D翻书效果
  10. 本地如何搭建FPT服务
  11. 6.Jenkins 权威指南 --- 高级构建
  12. android .9图片如何引用,Android调用相机拍照并返回路径和调用系统图库选择图片...
  13. CCNA学习指南 IP路由
  14. TextView常用属性设置
  15. 微信 分享领券 php,微信卡券货架显示已领取
  16. CentOS换源、linux配置IP、腾讯云SHH秘钥、公钥
  17. 网传互联网公司大裁员
  18. ubuntu 安装 mujoco-py
  19. 20款优秀的免费 WordPress 企业主题
  20. C++之我见--delete指针

热门文章

  1. centos locate搜索工具
  2. Recommender Systems协同过滤
  3. Java程序猿从笨鸟到菜鸟之(九十二)深入java虚拟机(一)——java虚拟机底层结构具体解释...
  4. 送6个Gmail邀请!
  5. [转载] python - map()解析
  6. [转载] python 语言基础 - 字符串常用函数及操作
  7. MySQL数据库篇之索引原理与慢查询优化之一
  8. 苹果被拒的血泪史。。。(update 2015.11)
  9. BZOJ-1507 文本编辑器(Editor)
  10. 开发WinRT自定义组件之富文本框