📜  如何在Python中对数字进行四舍五入?

📅  最后修改于: 2022-05-13 01:54:44.589000             🧑  作者: Mango

如何在Python中对数字进行四舍五入?

四舍五入意味着通过保持其值不变但更接近下一个数字来使数字更简单。
示例:如果我们想对一个数字进行四舍五入,比如 3.5。它将四舍五入到最接近的整数 4。但是,数字 3.74 将四舍五入到小数点后一位,得到 3.7。

方法一:使用 内置 圆形的() 函数。
在Python中有一个内置的 round()函数,可以将数字四舍五入到给定的位数。函数round() 接受两个数字参数,n 位和 n 位,然后将数字 n 四舍五入后返回 n 位。如果未提供用于舍入的位数,则该函数将给定数字 n 舍入为最接近的整数。

例子:

python3
# For integers
print(round(11))
  
# For floating point
print(round(22.7))  
  
# if the second parameter is present
  
# when the (ndigit+1)th digit is =5 
print(round(4.465, 2)) 
    
# when the (ndigit+1)th digit is >=5 
print(round(4.476, 2))   
    
# when the (ndigit+1)th digit is <5 
print(round(4.473, 2))


python3
# defining truncate function
# second argument defaults to 0
# so that if no argument is passed 
# it returns the integer part of number
  
def truncate(n, decimals = 0):
    multiplier = 10 ** decimals
    return int(n * multiplier) / multiplier
  
print(truncate(16.5))
print(truncate(-3.853, 1))
print(truncate(3.815, 2))
  
# we can truncate digits towards the left of the decimal point
# by passing a negative number.
print(truncate(346.8, -1))
print(truncate(-2947.48, -3))


python3
# import math library
import math
  
# ceil value for positive
# decimal number
print(math.ceil(4.2))
  
# ceil value for negative
# decimal number
print(math.ceil(-0.5))
  
# floor value for decimal 
# and negative number
print(math.floor(2.2))
print(math.floor(-0.5))


python3
# import math library
import math
  
# define a function for 
# round_up
def round_up(n, decimals = 0): 
    multiplier = 10 ** decimals 
    return math.ceil(n * multiplier) / multiplier
  
# passing positive values
print(round_up(2.1))
print(round_up(2.23, 1))
print(round_up(2.543, 2))
  
# passing negative values
print(round_up(22.45, -1))
print(round_up(2352, -2))


python3
import math
  
# defining a function for
# round down.
def round_down(n, decimals=0):
    multiplier = 10 ** decimals
    return math.floor(n * multiplier) / multiplier
  
# passing different values to function
print(round_down(2.5))
print(round_down(2.48, 1))
print(round_down(-0.5))


python3
import math
  
# defining round_half_up
  
def round_half_up(n, decimals=0):
    multiplier = 10 ** decimals
    return math.floor(n * multiplier + 0.5) / multiplier
  
# passing different values to the function
  
print(round_half_up(1.28, 1))
print(round_half_up(-1.5))
print(round_half_up(-1.225, 2))


python3
# import math library
import math
  
# defining a function
# for round_half_down
def round_half_down(n, decimals=0):
    multiplier = 10 ** decimals
    return math.ceil(n * multiplier - 0.5) / multiplier
  
# passing different values to the function
print(round_half_down(2.5))
print(round_half_down(-2.5))
print(round_half_down(2.25, 1))


python3
# import Decimal function from 
# decimal library
from decimal import Decimal
print(Decimal("0.1"))
print(Decimal(0.1))
  
# Rounding a Decimal number is
# done with the .quantize() function
# "1.0" in .quantize() determines the
# number of decimal places to round the number
print(Decimal("1.65").quantize(Decimal("1.0")))
print(Decimal("1.675").quantize(Decimal("1.00")))


输出:

11
23
4.46
4.48
4.47

方法二:使用截断概念。
截断是对数字进行舍入的最简单方法之一,它涉及将数字截断为给定位数。在此函数中,给定位置之后的每个数字都替换为 0。 truncate()函数可用于正数和负数。

使用 truncate() 用户定义函数实现截断概念:

蟒蛇3

# defining truncate function
# second argument defaults to 0
# so that if no argument is passed 
# it returns the integer part of number
  
def truncate(n, decimals = 0):
    multiplier = 10 ** decimals
    return int(n * multiplier) / multiplier
  
print(truncate(16.5))
print(truncate(-3.853, 1))
print(truncate(3.815, 2))
  
# we can truncate digits towards the left of the decimal point
# by passing a negative number.
print(truncate(346.8, -1))
print(truncate(-2947.48, -3))

输出:

16.0
-3.8
3.81
340.0
-2000.0

方法 3:使用数学ceil() 数学.地板() 职能。
Math.ceil():此函数返回大于或等于给定数字的最接近的整数。
Math.floor():此函数返回小于或等于给定数字的最接近的整数。
例子:

蟒蛇3

# import math library
import math
  
# ceil value for positive
# decimal number
print(math.ceil(4.2))
  
# ceil value for negative
# decimal number
print(math.ceil(-0.5))
  
# floor value for decimal 
# and negative number
print(math.floor(2.2))
print(math.floor(-0.5))

输出:

5
0
2
-1

方法 3:使用四舍五入的概念。
在四舍五入中,数字被四舍五入到指定的位数。

使用 round_up() 用户定义函数实现舍入概念:

蟒蛇3

# import math library
import math
  
# define a function for 
# round_up
def round_up(n, decimals = 0): 
    multiplier = 10 ** decimals 
    return math.ceil(n * multiplier) / multiplier
  
# passing positive values
print(round_up(2.1))
print(round_up(2.23, 1))
print(round_up(2.543, 2))
  
# passing negative values
print(round_up(22.45, -1))
print(round_up(2352, -2))

输出:

3.0
2.3
2.55
30.0
2400.0

我们可以按照下图来理解向上取整和向下取整。向右向上舍入,向左向下舍入。

了解向上舍入和向下舍入

向上舍入总是将数字轴上的数字四舍五入,向下舍入总是将数字轴上的数字四舍五入。
方法 5:使用四舍五入的概念。

在四舍五入中,数字被四舍五入到指定的位数。

使用 round_down() 用户定义函数实现舍入概念:

蟒蛇3

import math
  
# defining a function for
# round down.
def round_down(n, decimals=0):
    multiplier = 10 ** decimals
    return math.floor(n * multiplier) / multiplier
  
# passing different values to function
print(round_down(2.5))
print(round_down(2.48, 1))
print(round_down(-0.5))

输出:

2.0
2.4
-1.0

方法 5:使用舍入偏差概念。
对称性的概念引入了舍入偏差的概念,它描述了舍入如何影响数据集中的数值数据。
向上舍入策略具有向正无穷大偏差舍入的舍入,因为该值总是在正无穷大的方向上四舍五入。类似地,向下舍入策略有一个朝向负无穷大偏差的舍入。截断策略对正值有一轮朝负无穷大偏差,对于负值有一轮朝正无穷大。一般来说,具有这种行为的舍入函数被称为具有零偏差的舍入。

a) 四舍五入的概念。
四舍五入将每个数字四舍五入到具有指定精度的最接近的数字,并通过四舍五入打破平局。
通过将小数点向右移动所需的位数来实现四舍五入策略。在这种情况下,我们必须确定小数点后的数字是小于还是大于等于 5。
我们可以将 0.5 添加到移动的值,然后使用 math.floor()函数将其向下舍入。

round_half_up()函数的实现:

蟒蛇3

import math
  
# defining round_half_up
  
def round_half_up(n, decimals=0):
    multiplier = 10 ** decimals
    return math.floor(n * multiplier + 0.5) / multiplier
  
# passing different values to the function
  
print(round_half_up(1.28, 1))
print(round_half_up(-1.5))
print(round_half_up(-1.225, 2))

输出:

1.3
-1.0
-1.23

b) 四舍五入的概念。
这类似于四舍五入方法一样四舍五入到最接近的数字,不同之处在于它通过四舍五入到两个数字中的较小者来打破平局。舍入减半策略是通过用 math.ceil() 替换 round_half_up()函数中的 math.floor() 然后减去 0.5 而不是相加来实现的。

round_half_down()函数的实现:

蟒蛇3

# import math library
import math
  
# defining a function
# for round_half_down
def round_half_down(n, decimals=0):
    multiplier = 10 ** decimals
    return math.ceil(n * multiplier - 0.5) / multiplier
  
# passing different values to the function
print(round_half_down(2.5))
print(round_half_down(-2.5))
print(round_half_down(2.25, 1))

输出:

2.0
-3.0
2.2

方法 6:从零四舍五入。
在四舍五入中,我们需要做的是像往常一样开始将小数点向右移动给定的位数,然后注意新数字中小数点右侧的数字(d)。有四种情况需要考虑:

  • 如果 n 为正且 d >= 5,则向上取整
  • 如果 n 为正且 d = 5,则向下舍入
  • 如果 n 为负且 d >= 5,则向下舍入
  • 如果 n 为负且 d < 5,则向上取整

按照上面提到的规则四舍五入后,我们可以将小数点左移。

  • 四舍五入到偶数:当我们对数据集中的值进行舍入时,有一种方法可以减轻舍入偏差。我们可以简单地以所需的精度将平局四舍五入到最接近的偶数。舍入一半到偶数策略是 Python 内置的 round() 使用的策略。 decimal 类提供对快速正确舍入的十进制浮点运算的支持。这提供了优于浮点数据类型的几个优点。十进制模块中的默认舍入策略是 ROUND_HALF_EVEN。

例子:

蟒蛇3

# import Decimal function from 
# decimal library
from decimal import Decimal
print(Decimal("0.1"))
print(Decimal(0.1))
  
# Rounding a Decimal number is
# done with the .quantize() function
# "1.0" in .quantize() determines the
# number of decimal places to round the number
print(Decimal("1.65").quantize(Decimal("1.0")))
print(Decimal("1.675").quantize(Decimal("1.00")))

输出:

0.1
0.1000000000000000055511151231257827021181583404541015625
1.6
1.68