DAY4-step5 Python示例说明 round()函数
wptr33 2025-07-15 01:25 13 浏览
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
相关推荐
- redis的八种使用场景
-
前言:redis是我们工作开发中,经常要打交道的,下面对redis的使用场景做总结介绍也是对redis举报的功能做梳理。缓存Redis最常见的用途是作为缓存,用于加速应用程序的响应速度。...
- 基于Redis的3种分布式ID生成策略
-
在分布式系统设计中,全局唯一ID是一个基础而关键的组件。随着业务规模扩大和系统架构向微服务演进,传统的单机自增ID已无法满足需求。高并发、高可用的分布式ID生成方案成为构建可靠分布式系统的必要条件。R...
- 基于OpenWrt系统路由器的模式切换与网页设计
-
摘要:目前商用WiFi路由器已应用到多个领域,商家通过给用户提供一个稳定免费WiFi热点达到吸引客户、提升服务的目标。传统路由器自带的Luci界面提供了工厂模式的Web界面,用户可通过该界面配置路...
- 这篇文章教你看明白 nginx-ingress 控制器
-
主机nginx一般nginx做主机反向代理(网关)有以下配置...
- 如何用redis实现注册中心
-
一句话总结使用Redis实现注册中心:服务注册...
- 爱可可老师24小时热门分享(2020.5.10)
-
No1.看自己以前写的代码是种什么体验?No2.DooM-chip!国外网友SylvainLefebvre自制的无CPU、无操作码、无指令计数器...No3.我认为CS学位可以更好,如...
- Apportable:拯救程序员,IOS一秒变安卓
-
摘要:还在为了跨平台使用cocos2d-x吗,拯救objc程序员的奇葩来了,ApportableSDK:FreeAndroidsupportforcocos2d-iPhone。App...
- JAVA实现超买超卖方案汇总,那个最适合你,一篇文章彻底讲透
-
以下是几种Java实现超买超卖问题的核心解决方案及代码示例,针对高并发场景下的库存扣减问题:方案一:Redis原子操作+Lua脚本(推荐)//使用Redis+Lua保证原子性publicbo...
- 3月26日更新 快速施法自动施法可独立设置
-
2016年3月26日DOTA2有一个79.6MB的更新主要是针对自动施法和快速施法的调整本来内容不多不少朋友都有自动施法和快速施法的困扰英文更新日志一些视觉BUG修复就不翻译了主要翻译自动施...
- Redis 是如何提供服务的
-
在刚刚接触Redis的时候,最想要知道的是一个’setnameJhon’命令到达Redis服务器的时候,它是如何返回’OK’的?里面命令处理的流程如何,具体细节怎么样?你一定有问过自己...
- lua _G、_VERSION使用
-
到这里我们已经把lua基础库中的函数介绍完了,除了函数外基础库中还有两个常量,一个是_G,另一个是_VERSION。_G是基础库本身,指向自己,这个变量很有意思,可以无限引用自己,最后得到的还是自己,...
- China's top diplomat to chair third China-Pacific Island countries foreign ministers' meeting
-
BEIJING,May21(Xinhua)--ChineseForeignMinisterWangYi,alsoamemberofthePoliticalBureau...
- 移动工作交流工具Lua推出Insights数据分析产品
-
Lua是一个适用于各种职业人士的移动交流平台,它在今天推出了一项叫做Insights的全新功能。Insights是一个数据平台,客户可以在上面实时看到员工之间的交流情况,并分析这些情况对公司发展的影响...
- Redis 7新武器:用Redis Stack实现向量搜索的极限压测
-
当传统关系型数据库还在为向量相似度搜索的性能挣扎时,Redis7的RedisStack...
- Nginx/OpenResty详解,Nginx Lua编程,重定向与内部子请求
-
重定向与内部子请求Nginx的rewrite指令不仅可以在Nginx内部的server、location之间进行跳转,还可以进行外部链接的重定向。通过ngx_lua模块的Lua函数除了能实现Nginx...
- 一周热门
-
-
C# 13 和 .NET 9 全知道 :13 使用 ASP.NET Core 构建网站 (1)
-
因果推断Matching方式实现代码 因果推断模型
-
git pull命令使用实例 git pull--rebase
-
git pull 和git fetch 命令分别有什么作用?二者有什么区别?
-
面试官:git pull是哪两个指令的组合?
-
git 执行pull错误如何撤销 git pull fail
-
git fetch 和git pull 的异同 git中fetch和pull的区别
-
git pull 之后本地代码被覆盖 解决方案
-
还可以这样玩?Git基本原理及各种骚操作,涨知识了
-
git命令之pull git.pull
-
- 最近发表
- 标签列表
-
- 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)