DAY4-step5 Python示例说明 round()函数
wptr33 2025-07-15 01:25 22 浏览
Round()
Round()是python提供的内置函数。 它将返回一个浮点数,该浮点数将四舍五入到指定的精度。
如果未指定要舍入的小数位,则将其视为0,并将舍入到最接近的整数。
语法:
round(float_num, num_of_decimals)
参量
- float_num:要四舍五入的浮点数。
- num_of_decimals :(可选)舍入时要考虑的小数位数。 它是可选的,如果未指定,则默认为0,并且四舍五入到最接近的整数。
描述
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模块带有舍入类型,如下所示:
- ROUND_CEILING:它将向Infinity舍入,
- ROUND_DOWN:它将值舍入为零,
- ROUND_FLOOR:它将朝-Infinity取整,
- ROUND_HALF_DOWN:它将四舍五入为最接近零的值,
- ROUND_HALF_EVEN:它将舍入到最接近的值,其值将接近最接近的偶数整数,
- ROUND_HALF_UP:它将舍入到最接近的值,其值将远离零
- ROUND_UP:它将四舍五入到零值处。
以十进制表示时,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
相关推荐
- oracle数据导入导出_oracle数据导入导出工具
-
关于oracle的数据导入导出,这个功能的使用场景,一般是换服务环境,把原先的oracle数据导入到另外一台oracle数据库,或者导出备份使用。只不过oracle的导入导出命令不好记忆,稍稍有点复杂...
- 继续学习Python中的while true/break语句
-
上次讲到if语句的用法,大家在微信公众号问了小编很多问题,那么小编在这几种解决一下,1.else和elif是子模块,不能单独使用2.一个if语句中可以包括很多个elif语句,但结尾只能有一个...
- python continue和break的区别_python中break语句和continue语句的区别
-
python中循环语句经常会使用continue和break,那么这2者的区别是?continue是跳出本次循环,进行下一次循环;break是跳出整个循环;例如:...
- 简单学Python——关键字6——break和continue
-
Python退出循环,有break语句和continue语句两种实现方式。break语句和continue语句的区别:break语句作用是终止循环。continue语句作用是跳出本轮循环,继续下一次循...
- 2-1,0基础学Python之 break退出循环、 continue继续循环 多重循
-
用for循环或者while循环时,如果要在循环体内直接退出循环,可以使用break语句。比如计算1至100的整数和,我们用while来实现:sum=0x=1whileTrue...
- Python 中 break 和 continue 傻傻分不清
-
大家好啊,我是大田。...
- python中的流程控制语句:continue、break 和 return使用方法
-
Python中,continue、break和return是控制流程的关键语句,用于在循环或函数中提前退出或跳过某些操作。它们的用途和区别如下:1.continue(跳过当前循环的剩余部分,进...
- L017:continue和break - 教程文案
-
continue和break在Python中,continue和break是用于控制循环(如for和while)执行流程的关键字,它们的作用如下:1.continue:跳过当前迭代,...
- 作为前端开发者,你都经历过怎样的面试?
-
已经裸辞1个月了,最近开始投简历找工作,遇到各种各样的面试,今天分享一下。其实在职的时候也做过面试官,面试官时,感觉自己问的问题很难区分候选人的能力,最好的办法就是看看候选人的github上的代码仓库...
- 面试被问 const 是否不可变?这样回答才显功底
-
作为前端开发者,我在学习ES6特性时,总被const的"善变"搞得一头雾水——为什么用const声明的数组还能push元素?为什么基本类型赋值就会报错?直到翻遍MDN文档、对着内存图反...
- 2023金九银十必看前端面试题!2w字精品!
-
导文2023金九银十必看前端面试题!金九银十黄金期来了想要跳槽的小伙伴快来看啊CSS1.请解释CSS的盒模型是什么,并描述其组成部分。...
- 前端面试总结_前端面试题整理
-
记得当时大二的时候,看到实验室的学长学姐忙于各种春招,有些收获了大厂offer,有些还在苦苦面试,其实那时候的心里还蛮忐忑的,不知道自己大三的时候会是什么样的一个水平,所以从19年的寒假放完,大二下学...
- 由浅入深,66条JavaScript面试知识点(七)
-
作者:JakeZhang转发链接:https://juejin.im/post/5ef8377f6fb9a07e693a6061目录...
- 2024前端面试真题之—VUE篇_前端面试题vue2020及答案
-
添加图片注释,不超过140字(可选)...
- 今年最常见的前端面试题,你会做几道?
-
在面试或招聘前端开发人员时,期望、现实和需求之间总是存在着巨大差距。面试其实是一个交流想法的地方,挑战人们的思考方式,并客观地分析给定的问题。可以通过面试了解人们如何做出决策,了解一个人对技术和解决问...
- 一周热门
- 最近发表
-
- oracle数据导入导出_oracle数据导入导出工具
- 继续学习Python中的while true/break语句
- python continue和break的区别_python中break语句和continue语句的区别
- 简单学Python——关键字6——break和continue
- 2-1,0基础学Python之 break退出循环、 continue继续循环 多重循
- Python 中 break 和 continue 傻傻分不清
- python中的流程控制语句:continue、break 和 return使用方法
- L017:continue和break - 教程文案
- 作为前端开发者,你都经历过怎样的面试?
- 面试被问 const 是否不可变?这样回答才显功底
- 标签列表
-
- git pull (33)
- git fetch (35)
- mysql insert (35)
- mysql distinct (37)
- concat_ws (36)
- java continue (36)
- jenkins官网 (37)
- mysql 子查询 (37)
- python元组 (33)
- mybatis 分页 (35)
- vba split (37)
- redis watch (34)
- python list sort (37)
- nvarchar2 (34)
- mysql not null (36)
- hmset (35)
- python telnet (35)
- python readlines() 方法 (36)
- munmap (35)
- docker network create (35)
- redis 集合 (37)
- python sftp (37)
- setpriority (34)
- c语言 switch (34)
- git commit (34)