Round()是python提供的内置函数。 它将返回一个浮点数,该浮点数将四舍五入到指定的精度。
如果未指定要舍入的小数位,则将其视为0,并将舍入到最接近的整数。
语法:
round(float_num, num_of_decimals)
参量
描述
round()方法有两个参数
第二个参数是可选的,当未指定时默认为0,在这种情况下,它将四舍五入为最接近的整数,并且返回类型也将为整数。
如果出现小数位数(即第二个参数),则将舍入到给定的位数。返回类型将是浮点型。
返回值
如果未指定num_of_decimals,则返回整数值;如果给出num_of_decimals,则返回浮点值。请注意,如果小数点后的值> = 5,则该值将四舍五入为+1,否则它将返回该值。
在该程序中,我们将看到如何对浮点数取整
# 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
让我们看一些关于如何对负数取整的示例
# testing round()
num = -2.8
num1 = -1.5
print("The value after rounding is", round(num))
print("The value after rounding is", round(num1))
输出
The value after rounding is -3
The value after rounding is -2
如何在python中round一个numpy数组?
为了解决这个问题,我们可以使用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)
Output:
[-0.34 1.46 4.23 -0.34 7.63 5.12]
除了round()函数外,python还有一个十进制模块,可帮助更准确地处理十进制数字。
Decimal模块带有舍入类型,如下所示:
以十进制表示时,quantumize()方法有助于四舍五入到固定数量的小数位,您可以指定要使用的舍入,如下例所示。
例:
使用round()和十进制方法
import decimal
round_num = 15.456
final_val = round(round_num, 2)
#使用十进制模块
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)
Output:
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