DAY4-step5 Python示例说明 round()函数
itomcoil 2025-05-23 17:48 3 浏览
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
相关推荐
- Python 最常用的语句、函数有哪些?
-
1.#coding=utf-8①代码中有中文字符,最好在代码前面加#coding=utf-8②pycharm不加可能不会报错,但是代码最终是会放到服务器上,放到服务器上的时候运行可能会报错。③...
- PyYAML 实用的使用技巧
-
作者:Reorx’sForge中文版:https://reorx.com/blog/python-yaml-tips-zh英文版:Tipsthatmaysaveyoufromthehe...
- 学习编程第127天 python中字符串与数值中的巧妙应用
-
今天学习的刘金玉老师零基础Python教程第10期,主要内容是python中字符串与数值中的巧妙应用。一、新建一个工程如图,新建一个工程。二、字符串与数值的区别变量只有为数值的时候,才能进行数学运算。...
- Python 必学!12 个 “开挂级” 内置函数深度解析(小白也能秒懂)
-
干货来了以下是Python中12个强大内置函数的深度解析,涵盖数据处理、代码优化和高级场景,助你写出更简洁高效的代码:一、数据处理三剑客1.map(function,iterable)作用:...
- Python浮点数保留两位小数的方法
-
技术背景在Python编程中,经常会遇到需要将浮点数保留特定小数位数的情况,比如在处理货币、统计数据等场景。然而,由于浮点数在计算机中采用二进制表示,存在精度问题,导致直接使用round函数有时无法得...
- DAY4-step5 Python示例说明 round()函数
-
Round()Round()是python提供的内置函数。它将返回一个浮点数,该浮点数将四舍五入到指定的精度。如果未指定要舍入的小数位,则将其视为0,并将舍入到最接近的整数。语法:round(flo...
- 第五个测试版本了,iOS 9 又有了什么变化?
-
今天的早些时候苹果发布了iOS9beta5,除去修复BUG和提升系统的稳定性外,苹果还带来了一些新功能。本次更新包括了对Carplay,WiFi以及Siri等功能的优化,Mac...
- 如何在 Python 中随机排列列表元素
-
在本教程中,我们将学习在Python中如何打乱列表元素顺序,随机排列列表元素。如何随机排列列表是一项非常有用的技能。它在开发需要选择随机结果游戏中非常有用。它还适用于数据相关的工作中,可能需要提取...
- Python获取随机数方法汇总
-
1.random.random()作用:随机生成一个[0,1)之间的浮点数importrandomprint(f'随机生成一个[0,1)之间的浮点数={random.random()}&...
- Python程序开发之简单小程序实例(11)小游戏-跳动的小球
-
Python程序开发之简单小程序实例(11)小游戏-跳动的小球一、项目功能用户控制挡板来阻挡跳动的小球。二、项目分析根据项目功能自定义两个类,一个用于控制小球在窗体中的运动,一个用于接收用户按下左右键...
- Keras人工智能神经网络 Regressor 回归 神经网络搭建
-
前期分享了使用tensorflow来进行神经网络的回归,tensorflow构建神经网络本期我们来使用Keras来搭建一个简单的神经网络Keras神经网络可以用来模拟回归问题(regression)...
- 我让DeepSeek写程序,有懂的看看写的对不对?
-
他写的时候就像教学生解方程一样,解释一段写一段,因为中间太长,我就截了最后的结果,应该是手机版本复制字数有限,可能也没复制全,有没有懂的看看写的对不对?下面是他写的最后结果。importrandom...
- Picker Wheel 线上随机抽签轮盘
-
#头条创作挑战赛#办公室经常会碰到「中午要吃什么?」、「要订哪家饮料店?」或「谁要去帮大家跑腿?」等各种情境,为了公平起见,我们可以使用随机方式进行抽签,这样一来就能确保公平公正性,其他人也就不...
- 思维链COT原理探究
-
TEXTANDPATTERNS:FOREFFECTIVECHAINOFTHOUGHTITTAKESTWOTOTANGO测试模型:PaLM-62B,GPT3,CODEXCOT元素...
- 永别了iPod!系列产品回顾,你用过几款?
-
中关村在线消息:就在本周,苹果官方宣布iPod系列将不再生产新品,现货售完即止。作为一个偏向音乐播放的系列,iPod系列想必陪伴了很多朋友的学生年代。近日有外媒总结了iPod系列的全部产品,来看看你用...
- 一周热门
- 最近发表
- 标签列表
-
- ps图案在哪里 (33)
- super().__init__ (33)
- python 获取日期 (34)
- 0xa (36)
- super().__init__()详解 (33)
- python安装包在哪里找 (33)
- linux查看python版本信息 (35)
- python怎么改成中文 (35)
- php文件怎么在浏览器运行 (33)
- eval在python中的意思 (33)
- python安装opencv库 (35)
- python div (34)
- sticky css (33)
- python中random.randint()函数 (34)
- python去掉字符串中的指定字符 (33)
- python入门经典100题 (34)
- anaconda安装路径 (34)
- yield和return的区别 (33)
- 1到10的阶乘之和是多少 (35)
- python安装sklearn库 (33)
- dom和bom区别 (33)
- js 替换指定位置的字符 (33)
- python判断元素是否存在 (33)
- sorted key (33)
- shutil.copy() (33)