Python round() 函数及其示例

round() 函数是什么? Python?
Python round() 是一个内置函数,可通过以下方式使用: Python它将返回一个浮点数,该浮点数将四舍五入到输入的小数位数。
若未指定要四舍五入的小数位数,则视为 0,并四舍五入为最接近的整数。
句法
round(float_num, num_of_decimals)
参数
- float_num: 要进行四舍五入的浮点数。
- 小数位数: (可选)舍入时保留的小数位数。此项为可选参数,如果未指定,则默认为 0,并舍入到最接近的整数。
描述
round() 方法接受两个参数:
- 要四舍五入的数字,以及
- 四舍五入时应考虑的小数位数。
第二个参数是可选的,如果未指定则默认为 0,在这种情况下,它将四舍五入到最接近的整数,并且返回类型也将是整数。
当有小数位(即第二个参数)时,它将四舍五入到给定的位数。返回类型为浮点数。
如果给定小数点后一位的数字是:
- 如果大于等于 5,则最终值加 1。
- 如果小于 5,则最终值将按原样返回,精确到指定的小数位数。
返回值
如果未指定 `num_of_decimals` 参数,则返回整数值;如果指定了 `num_of_decimals` 参数,则返回浮点数值。请注意,如果小数点后的数字大于等于 5,则该值将四舍五入到 +1;否则,将按原样返回该值,直至指定的小数位数。
四舍五入能产生多大影响?(四舍五入与截断)
要充分展现四舍五入的影响,股票交易市场就是一个绝佳的例子。在过去,例如1982年, 温哥华证券交易所 (VSE)用于将每次交易的股票价值截断为小数点后三位。
每天发生近3000次这样的操作。累积起来的截断导致每月损失约25分。
下面显示了截断值与四舍五入的示例。
将下面生成的浮点数视为股票价值。目前,我生成的数值范围为 1,000,000 秒,介于 0.01 和 0.05 之间。
例子:
arr = [random.uniform(0.01, 0.05) for _ in range(1000000)]
为了展示舍入的影响,我编写了一小段代码,其中,首先只使用小数点后 3 位的数字,即截断小数点后 3 位的数字。
我得到了原始总值、截断值后的总值,以及原始值与截断值之间的差值。
对于同一组数字,我使用了 round() 方法,保留三位小数,并计算了原始值与四舍五入值之间的总和和差值。
以下是示例和输出结果。
例子1
import random def truncate(num): return int(num * 1000) / 1000 arr = [random.uniform(0.01, 0.05) for _ in range(1000000)] sum_num = 0 sum_truncate = 0 for i in arr: sum_num = sum_num + i sum_truncate = truncate(sum_truncate + i) print("Testing by using truncating upto 3 decimal places") print("The original sum is = ", sum_num) print("The total using truncate = ", sum_truncate) print("The difference from original - truncate = ", sum_num - sum_truncate) print("\n\n") print("Testing by using round() upto 3 decimal places") sum_num1 = 0 sum_truncate1 = 0 for i in arr: sum_num1 = sum_num1 + i sum_truncate1 = round(sum_truncate1 + i, 3) print("The original sum is =", sum_num1) print("The total using round = ", sum_truncate1) print("The difference from original - round =", sum_num1 - sum_truncate1)
输出:
Testing by using truncating upto 3 decimal places The original sum is = 29985.958619386867 The total using truncate = 29486.057 The difference from original - truncate = 499.9016193868665 Testing by using round() up to 3 decimal places The original sum is = 29985.958619386867 The total using round = 29985.912 The difference from original - round = 0.04661938686695066
原始值与截断值之间的差值为 499.9016193868665,而 round() 的值为 0.04661938686695066。
差异似乎非常大,该示例表明 round() 方法如何帮助计算出接近精确值的结果。
示例:浮点数舍入 Numbers
在这个程序中,我们将看到浮点数的舍入是如何进行的。
# testing round() float_num1 = 10.60 # here the value will be rounded to 11 as after the decimal point the number is 6 that is >5 float_num2 = 10.40 # here the value will be rounded to 10 as after the decimal point the number is 4 that is <=5 float_num3 = 10.3456 # here the value will be 10.35 as after the 2 decimal points the value >=5 float_num4 = 10.3445 #here the value will be 10.34 as after the 2 decimal points the value is <5 print("The rounded value without num_of_decimals is :", round(float_num1)) print("The rounded value without num_of_decimals is :", round(float_num2)) print("The rounded value with num_of_decimals as 2 is :", round(float_num3, 2)) print("The rounded value with num_of_decimals as 2 is :", round(float_num4, 2))
输出:
The rounded value without num_of_decimals is : 11 The rounded value without num_of_decimals is : 10 The rounded value with num_of_decimals as 2 is : 10.35 The rounded value with num_of_decimals as 2 is : 10.34
示例:对整数值进行舍入
如果您碰巧对整数值使用 round(),它将直接返回该数字而不做任何更改。
# testing round() on a integer num = 15 print("The output is", round(num))
输出:
The output is 15
示例:对负数进行舍入 Numbers
让我们来看几个负数四舍五入的例子。
# testing round() num = -2.8 num1 = -1.5 print("The value after rounding is", round(num)) print("The value after rounding is", round(num1))
输出:
C:\pythontest>python testround.py The value after rounding is -3 The value after rounding is -2
示例:对 NumPy 数组进行四舍五入
如何对 NumPy 进行四舍五入 数组 Python?
为了解决这个问题,我们可以使用 NumPy 模块,并使用 numpy.round() 或 numpy.around() 方法,如下例所示。
使用 numpy.round()
# testing round() import numpy as np arr = [-0.341111, 1.455098989, 4.232323, -0.3432326, 7.626632, 5.122323] arr1 = np.round(arr, 2) print(arr1)
输出:
C:\pythontest>python testround.py [-0.34 1.46 4.23 -0.34 7.63 5.12]
我们还可以使用 numpy.around(),它能得到与上面显示的 numpy.round() 相同的结果。
例如:十进制模块
除了 round() 函数之外, Python 它具有十进制模块,可以帮助更准确地处理十进制数。
Decimal 模块带有舍入类型,如下所示:
- 圆顶: 它将向无穷大方向取整。
- 向下取整: 它会将数值四舍五入到零。
- 圆形地板: 它将向负无穷大方向取整。
- 向下取整: 它将四舍五入到最接近零的值。
- ROUND_HALF_EVEN: 它将四舍五入到最接近的偶数。
- ROUND_HALF_UP: 它将四舍五入到最接近的远离零的值。
- 围捕: 当数值偏离零时,它会进行四舍五入。
在十进制中,quantize() 方法有助于四舍五入到固定的小数位数,并且您可以指定要使用的舍入,如下例所示。
计费示例: 使用 round() 和 decimal 方法
import decimal round_num = 15.456 final_val = round(round_num, 2) #Using decimal module final_val1 = decimal.Decimal(round_num).quantize(decimal.Decimal('0.00'), rounding=decimal.ROUND_CEILING) final_val2 = decimal.Decimal(round_num).quantize(decimal.Decimal('0.00'), rounding=decimal.ROUND_DOWN) final_val3 = decimal.Decimal(round_num).quantize(decimal.Decimal('0.00'), rounding=decimal.ROUND_FLOOR) final_val4 = decimal.Decimal(round_num).quantize(decimal.Decimal('0.00'), rounding=decimal.ROUND_HALF_DOWN) final_val5 = decimal.Decimal(round_num).quantize(decimal.Decimal('0.00'), rounding=decimal.ROUND_HALF_EVEN) final_val6 = decimal.Decimal(round_num).quantize(decimal.Decimal('0.00'), rounding=decimal.ROUND_HALF_UP) final_val7 = decimal.Decimal(round_num).quantize(decimal.Decimal('0.00'), rounding=decimal.ROUND_UP) print("Using round()", final_val) print("Using Decimal - ROUND_CEILING ",final_val1) print("Using Decimal - ROUND_DOWN ",final_val2) print("Using Decimal - ROUND_FLOOR ",final_val3) print("Using Decimal - ROUND_HALF_DOWN ",final_val4) print("Using Decimal - ROUND_HALF_EVEN ",final_val5) print("Using Decimal - ROUND_HALF_UP ",final_val6) print("Using Decimal - ROUND_UP ",final_val7)
输出:
Using round() 15.46 Using Decimal - ROUND_CEILING 15.46 Using Decimal - ROUND_DOWN 15.45 Using Decimal - ROUND_FLOOR 15.45 Using Decimal - ROUND_HALF_DOWN 15.46 Using Decimal - ROUND_HALF_EVEN 15.46 Using Decimal - ROUND_HALF_UP 15.46 Using Decimal - ROUND_UP 15.46
