如何在 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 模組,它允許我們將給定的浮點數轉換為十進位制數並向上取整。

更新於:2023 年 10 月 26 日

24K+ 次檢視

啟動您的 職業生涯

透過完成課程獲得認證

開始
廣告

© . All rights reserved.