Python round() 函数示例
圆形的()
Round() 是 Python 内置的一个函数。它将返回一个浮点数,该浮点数将被四舍五入到输入的小数位。
若未指定要四舍五入的小数位数,则视为 0,并四舍五入为最接近的整数。
句法
round(float_num, num_of_decimals)
参数
- float_num:需要四舍五入的浮点数。
- num_of_decimals:(可选)舍入时要考虑的小数位数。它是可选的,如果未指定,则默认为 0,并且舍入为最接近的整数。
描述
round() 方法接受两个参数
- 要四舍五入的数字和
- 四舍五入时应考虑的小数位数。
第二个参数是可选的,未指定时默认为0,此时将四舍五入到最接近的整数,返回类型也将是整数。
当有小数位(即第二个参数)时,它将四舍五入到给定的位数。返回类型为浮点数。
如果小数点后的数字
- >=5 比 +1 将被添加到最终值
- <5 则最终值将返回,因为它达到了提到的小数位。
返回值
如果未指定 num_of_decimals,它将返回一个整数值;如果指定了 num_of_decimals,它将返回一个浮点值。请注意,如果小数点后的值 >=1,则该值将四舍五入为 +5,否则它将按所提到的小数位数返回该值。
四舍五入能产生多大影响?(四舍五入与截断)
最能体现舍入影响的例子就是股票市场。过去,即 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() 方法精确到小数点后 3 位,并计算原始值与舍入值之间的总和与差。
以下是示例和输出
例子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,四舍五入为 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(),它会给出与下面示例相同的结果。
例如:十进制模块
除了 round() 函数之外,python 还有一个 decimal 模块,可以帮助更准确地处理十进制数。
Decimal 模块带有舍入类型,如下所示:
- ROUND_CEILING:它将向无穷大方向舍入,
- ROUND_DOWN:它将把值向零舍入,
- ROUND_FLOOR:它将向负无穷方向舍入,
- ROUND_HALF_DOWN:它将四舍五入到最接近零的值,
- ROUND_HALF_EVEN:它将四舍五入到最接近的偶数,
- ROUND_HALF_UP:它将四舍五入到最接近零的值。
- ROUND_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
总结
- Round(float_num, Num_of_decimals) 是 Python 内置函数。它将返回输入的四舍五入到小数位的浮点数。
- float_num:需要四舍五入的浮点数。
- Num_of_decimals:四舍五入时要考虑的小数位数。
- 如果未给出 num_of_decimals,它将返回一个整数值;如果给出了 num_of_decimals,则返回一个浮点值。