DAY4-step5 Python示例说明 round()函数
wptr33 2025-07-15 01:25 1 浏览
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
相关推荐
- SQL轻松入门(5):窗口函数(sql语录中加窗口函数的执行)
-
01前言标题中有2个字让我在初次接触窗口函数时,真真切切明白了何谓”高级”?说来也是一番辛酸史!话说,我见识了窗口函数的强大后,便磨拳擦掌的要试验一番,结果在查询中输入语句,返回的结果却是报错,Wh...
- 28个SQL常用的DeepSeek提示词指令,码住直接套用
-
自从DeepSeek出现后,极大地提升了大家平时的工作效率,特别是对于一些想从事数据行业的小白,只需要掌握DeepSeek的提问技巧,SQL相关的问题也不再是个门槛。...
- 从零开始学SQL进阶,数据分析师必备SQL取数技巧,建议收藏
-
上一节给大家讲到SQL取数的一些基本内容,包含SQL简单查询与高级查询,需要复习相关知识的同学可以跳转至上一节,本节给大家讲解SQL的进阶应用,在实际过程中用途比较多的子查询与窗口函数,下面一起学习。...
- SQL_OVER语法(sql语句over什么含义)
-
OVER的定义OVER用于为行定义一个窗口,它对一组值进行操作,不需要使用GROUPBY子句对数据进行分组,能够在同一行中同时返回基础行的列和聚合列。...
- SQL窗口函数知多少?(sql窗口怎么执行)
-
我们在日常工作中是否经常会遇到需要排名的情况,比如:每个部门按业绩来排名,每人按绩效排名,对部门销售业绩前N名的进行奖励等。面对这类需求,我们就需要使用sql的高级功能——窗口函数。...
- 如何学习并掌握 SQL 数据库基础:从零散查表到高效数据提取
-
无论是职场数据分析、产品运营,还是做副业项目,掌握SQL(StructuredQueryLanguage)意味着你能直接从数据库中提取、分析、整合数据,而不再依赖他人拉数,节省大量沟通成本,让你...
- SQL窗口函数(sql窗口函数执行顺序)
-
背景在数据分析中,经常会遇到按某某条件来排名、并找出排名的前几名,用日常SQL的GROUPBY,ORDERBY来实现特别的麻烦,有时甚至实现不了,这个时候SQL窗口函数就能发挥巨大作用了,窗...
- sqlserver删除重复数据只保留一条,使用ROW_NUMER()与Partition By
-
1.使用场景:公司的小程序需要实现一个功能:在原有小程序上,有一个优惠券活动表。存储着活动产品数据,但因为之前没有做约束,导致数据的不唯一,这会使打开产品详情页时,可能会出现随机显示任意活动问题。...
- SQL面试经典问题(一)(sql经典面试题及答案)
-
以下是三个精心挑选的经典SQL面试问题及其详细解决方案,涵盖了数据分析、排序限制和数据清理等常见场景。这些问题旨在考察SQL的核心技能,适用于初学者到高级开发者的面试准备。每个问题均包含清晰的...
- SQL:求连续N天的登陆人员之通用解答
-
前几天发了一个微头条:...
- SQL四大排序函数神技(sql中的排序是什么语句)
-
在日常SQL开发中,排序操作无处不在。当大家需要排序时,是否只会想到ORDERBY?今天,我们就来揭秘SQL中四个强大却常被忽略的排序函数:ROW_NUMBER()、RANK()、DENSE_RAN...
- 四、mysql窗口函数之row_number()函数的使用
-
1、窗口函数之row_number()使用背景窗口函数中,排序函数rank(),dense_rank()虽说都是排序函数,但是各有用处,假如像上章节说的“同组同分”两条数据,我们不想“班级名次”出现“...
- ROW_NUMBER()函数(rownumber函数与rank区别)
-
ROW_NUMBER()是SQL中的一个窗口函数(WindowFunction)...
- Dify「模板转换」节点终极指南:动态文本生成进阶技巧(附代码)Jinja2引擎解析
-
这篇文章是关于Dify「模板转换」节点的终极指南,解析了基于Jinja2模板引擎的动态文本生成技巧,涵盖多源文本整合、知识检索结构化、动态API构建及个性化内容生成等六大应用场景,助力开发者高效利用模...
- Python 最常用的语句、函数有哪些?
-
1.#coding=utf-8①代码中有中文字符,最好在代码前面加#coding=utf-8②pycharm不加可能不会报错,但是代码最终是会放到服务器上,放到服务器上的时候运行可能会报错。③...
- 一周热门
-
-
C# 13 和 .NET 9 全知道 :13 使用 ASP.NET Core 构建网站 (1)
-
因果推断Matching方式实现代码 因果推断模型
-
git pull命令使用实例 git pull--rebase
-
git 执行pull错误如何撤销 git pull fail
-
面试官:git pull是哪两个指令的组合?
-
git pull 和git fetch 命令分别有什么作用?二者有什么区别?
-
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)