如何在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模組,它允許我們將給定的浮點數轉換為十進位制數並對其進行向上取整。
資料結構
網路
關係資料庫管理系統(RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP