如何使用 Python 將浮點數向下舍入到小數點後 2 位?
在本文中,我們將向您展示如何在 Python 中將浮點數四捨五入到2位小數。以下是完成此任務的各種方法
使用 round() 函式
使用 format() 函式
使用 Decimal 模組
使用 ceil() 函式
使用 round() 函式
round() 函式 提供一個帶有指定小數位數的浮點數,它是指定數字的四捨五入版本。
由於小數位數的預設值為 0,因此該函式將返回最接近的整數。
語法
round(number, digits)
引數
number(必填) - 應四捨五入的數字
digits(可選) - 要四捨五入的小數位數。預設為 0。
演算法(步驟)
以下是執行所需任務的演算法/步驟 -。
建立一個變數來儲存輸入數字。
將輸入number、2(小數值)作為引數傳遞給 round() 函式,以將輸入數字四捨五入到小數點後 2 位。
列印輸入浮點數四捨五入到2位小數後的值。
示例
以下程式使用 round() 函式返回輸入浮點數四捨五入到小數點後 2 位的值 -
# input floating-point number inputNumber = 3.367824 # rounding the number up to 2 decimal places roundedNumber = round(inputNumber, 2) # print the rounded value of floating-point number up to 2 decimals print("Rounding 3.367824 upto 2 decimal places:", roundedNumber)
輸出
執行上述程式後,將生成以下輸出 -
Rounding 3.367824 upto 2 decimal places: 3.37
使用 format() 函式
演算法(步驟)
以下是執行所需任務的演算法/步驟 -。
建立一個變數來儲存輸入浮點數。
使用 format() 函式(它根據格式說明符返回指定輸入值的格式化版本)透過將輸入數字、格式(最多到小數點後 2 位)作為引數傳遞給它來將數字四捨五入到小數點後 2 位。
示例
以下程式使用 format() 函式返回輸入浮點數四捨五入到小數點後 2 位的值 -
# input floating-point number inputNumber = 4.56782 # rounding the number upto 2 decimal places print("Rounding 4.56782 upto 2 decimal places:", format(inputNumber,".2f"))
輸出
執行上述程式後,將生成以下輸出 -
Rounding 4.56782 upto 2 decimal places: 4.57
使用 Decimal 模組
Python 的decimal 模組有助於提高浮點數的精度。要使用此decimal模組,我們必須匯入它。
decimal.Decimal(floatnumber) - 預設情況下提供一個 50 位小數的值。
這裡我們使用value.quantize(decimal.Decimal('0.00')) 將小數點後四捨五入到2位。
演算法(步驟)
以下是執行所需任務的演算法/步驟 -。
使用 import 關鍵字匯入decimal模組。
建立一個變數來儲存輸入浮點數。
使用decimal模組的 Decimal() 函式轉換給定的浮點數。
使用 value.quantize(decimal.Decimal()) 函式將數字四捨五入到小數點後 2 位(這裡是小數點後 2 位,因此我們在小數點後給出 2 個零)。
列印輸入浮點數
列印輸入數字四捨五入到小數點後 2 位的值。
示例
以下程式使用 decimal 模組返回輸入浮點數四捨五入到小數點後 2 位的值 -
# importing decimal module import decimal # input floating-point number inputNumber = 35.65782 # Converting the given number to decimal decimalValue = decimal.Decimal(inputNumber) # rounding the number upto 2 digits after the decimal point roundedNumber = decimalValue.quantize(decimal.Decimal('0.00')) # printing the input floating-point number print("Input floating-point number: ", inputNumber) # printing the rounded value of the input number upto 2 decimal places print("Rounding 35.65782 upto 2 decimal places:", roundedNumber)
輸出
執行上述程式後,將生成以下輸出 -
Input floating-point number: 35.65782 Rounding 35.65782 upto 2 decimal places: 35.66
使用 ceil() 函式
演算法(步驟)
以下是執行所需任務的演算法/步驟 -。
使用 import 關鍵字匯入math 模組。
建立一個變數來儲存輸入浮點數。
使用ceil() 函式(返回數字的上限值,即大於或等於數字的最小整數)將數字四捨五入到小數點後 2 位,並列印結果數字。
If you want to round upto 1, 2, 3, 4...decimals then multiply and divide by # 10, 100, 1000, 10000 numbers respectively
示例
以下程式使用 ceil() 函式返回輸入浮點數四捨五入到小數點後 2 位的值 -
# importing math module import math # input floating-point number inputNumber = 4.56782 # rounding the number upto 2 decimal places using ceil() function # If you want to round upto 1, 2, 3, 4...decimals then multiply and divide by # 10, 100, 1000, 10000 numbers respectively print("Rounding 4.56782 upto 2 decimal places:") print(math.ceil(inputNumber*100)/100)
輸出
執行上述程式後,將生成以下輸出 -
Input floating-point number: 35.65782 Rounding 4.56782 upto 2 decimal places: 4.57
結論
在本文中,我們學習瞭如何使用四種不同的方法將給定的浮點數在Python中四捨五入到兩位小數。使用 ceil 函式和一些數學邏輯,我們學習瞭如何四捨五入到兩位小數。我們還學習瞭如何使用 decimal 模組將給定的浮點數轉換為十進位制數並對其進行量化。
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP