如何在 Python 中向上取整浮点数?
在这篇文章中,我们将向您展示如何在 Python 中向上取整浮点数。
使用 round() 函数向上取整浮点数
round() 函数 返回一个具有指定小数位数的浮点数,它是指定数字的四舍五入版本。
由于小数位数的默认值为 0,因此该函数将返回最接近的整数。
语法
round(number, digits)
参数
number(必填) - 需要四舍五入的数字
digits(可选) - 要四舍五入的小数位数。默认为 0。
Python 具有一个内置函数 round() 用于此目的。该函数接受两个参数,要四舍五入的数字以及要四舍五入到的位数。如果要将数字四舍五入到最接近的整数,则不提供第二个参数。
算法(步骤)
以下是执行所需任务的算法/步骤:
创建一个变量来存储输入数字。
使用 round() 函数通过将输入数字作为参数传递来向上取整输入数字。
示例
以下程序使用 round() 函数返回输入数字的向上取整值:
# input number inputNum = 2.14357 # rounding up the input number using the round() function print("rounding up", inputNum,":", round(inputNum))
输出
执行上述程序将生成以下输出:
rounding up 2.14357 : 2
在向上取整之前向 round() 函数添加 0.5
在这种方法中,在使用 round() 函数向上取整之前,将0.5添加到数字中
示例
# input number inputNum = 2.14357 # adding 0.5 to the input number and then rounding up using the round() function print("Adding 0.5 to", inputNum,"and then rounding up:", round(inputNum+0.5))
输出
执行上述程序将生成以下输出:
Adding 0.5 to 2.14357 and then rounding up: 3
我们取一个浮点数,例如 2.143,然后向其添加 0.5(使数字 2.6143>2.5),然后再将其作为参数传递给 round() 函数。因此,round() 函数在这种情况下将此整数四舍五入到上限,因为它超过了一半,即 2.5,因此结果为 3。
使用 ceil() 函数向上取整浮点数
在 Python 中,方法 ceil(x) 返回大于或等于 x 的最小整数。它称为 x 的上限值。
语法
import math math.ceil(x)
参数
x - 任何实数
返回值 - 返回不小于 x 的最小整数。
算法(步骤)
以下是执行所需任务的算法/步骤:
使用 import 关键字导入 math 模块。
使用 math.ceil() 函数获取数字的上限值,即大于或等于该数字的最小整数,方法是将该数字作为参数传递给它。
示例
以下程序返回 Python 中给定浮点数的向上取整值。
# importing math module import math # getting the ceiling value of numbers using math.ceil() print("Round-Up value of -12.11:", math.ceil(-12.11)) print("Round-Up value of 50.26:", math.ceil(50.26)) print("Round-Up value of 30.5:", math.ceil(30.5)) print("Round-Up value of 1.1:", math.ceil(1.1))
输出
执行上述程序将生成以下输出:
Round-Up value of -12.11: -12 Round-Up value of 50.26: 51 Round-Up value of 30.5: 31 Round-Up value of 1.1: 2
使用布尔逻辑向上取整浮点数
示例
以下程序使用 bool() 函数返回输入数字的向上取整值:
# input number n = 3.4 # rounding up the number print("rounding up the value of", n,":",int(n) + bool(n%1))
输出
执行上述程序将生成以下输出:
rounding up the value of 3.4: 4
使用 Decimal() 函数向上取整浮点数
算法(步骤)
以下是执行所需任务的算法/步骤:
使用 import 关键字从 decimal 模块导入所有函数(要导入所有函数,我们使用 * 运算符)。
使用 decimal() 函数(默认情况下提供 50 位小数的值)将给定数字转换为十进制数字,然后使用 quantize() 函数对其进行量化。
示例
以下程序使用 decimal 模块的 Decimal() 函数返回输入浮点数的向上取整值:
# importig all functions from the decimal module from decimal import * # rounding up the number print("the Round up value of 5.834 =",int(Decimal(5.834).quantize(Decimal('1.'), rounding=ROUND_UP)))
输出
执行上述程序将生成以下输出:
the Round up value of 5.834 = 6
使用 int() 函数向上取整浮点数
示例
以下程序使用 int() 函数 返回输入浮点数的向上取整值:
# input number n = 115.914 # rounding up the number print("Round up value of ",n,"is",int(n) + ((int(n) - n) != 0))
输出
执行上述程序将生成以下输出:
Round up value of 115.914 is 116
使用非运算符向上取整浮点数
示例
以下程序使用 int() 和 is_integer() 函数返回输入浮点数的向上取整值:
# input number n = 5.3 # rounding up the number result = int(n) + (not n.is_integer()) # priting the resultant rounded-up number print("rounding up value of", n,":", result)
输出
执行上述程序将生成以下输出:
rounding up value of 5.3 : 6
结论
在本教程中,我们介绍了如何在 Python 中使用多种方法向上取整给定的浮点数。通过使用示例,我们还了解了 round() 方法,该方法用于向上取整指定的整数。我们还了解了 decimal 模块,它允许我们将给定的浮点数转换为十进制数并向上取整。
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP