python round_python round()

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 = 0

    def __init__(self, i):
        self.id = i

    def __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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值