python保留小數(shù)點位數(shù)的多種方式(附demo)
前言
在Python中,保留小數(shù)點后特定位數(shù)可以通過多種方式實現(xiàn)
以下是幾種常見的方法,并附上相應(yīng)的代碼示例:
- 使用字符串格式化(String Formatting)
- 使用round()函數(shù)
- 使用Decimal模塊
- 使用numpy庫
1. 字符串格式
方法1:使用f-strings (Python 3.6及以上)
value = 3.141592653589793
formatted_value = f"{value:.2f}"
print(formatted_value) # 輸出: 3.14
方法2:使用str.format()
value = 3.141592653589793
formatted_value = "{:.2f}".format(value)
print(formatted_value) # 輸出: 3.14
方法3:使用百分號 (%) 格式化
value = 3.141592653589793 formatted_value = "%.2f" % value print(formatted_value) # 輸出: 3.14
2. round函數(shù)
value = 3.141592653589793 rounded_value = round(value, 2) print(rounded_value) # 輸出: 3.14
3. Decimal模塊
Decimal模塊提供更高的精度和控制,可以精確控制小數(shù)點后的位數(shù)
from decimal import Decimal, ROUND_HALF_UP
value = Decimal('3.141592653589793')
rounded_value = value.quantize(Decimal('0.01'), rounding=ROUND_HALF_UP)
print(rounded_value) # 輸出: 3.14
4. numpy庫
大量的數(shù)值計算,使用numpy庫是個好選擇
import numpy as np value = 3.141592653589793 rounded_value = np.round(value, 2) print(rounded_value) # 輸出: 3.14
5. Demo
總體Demo如下:
import numpy as np
from decimal import Decimal, ROUND_HALF_UP
value = 3.141592653589793
# 使用f-strings
formatted_value_f = f"{value:.2f}"
print(f"f-strings: {formatted_value_f}")
# 使用str.format()
formatted_value_format = "{:.2f}".format(value)
print(f"str.format(): {formatted_value_format}")
# 使用百分號 (%) 格式化
formatted_value_percent = "%.2f" % value
print(f"百分號格式化: {formatted_value_percent}")
# 使用round()函數(shù)
rounded_value_round = round(value, 2)
print(f"round(): {rounded_value_round}")
# 使用Decimal模塊
decimal_value = Decimal('3.141592653589793')
rounded_value_decimal = decimal_value.quantize(Decimal('0.01'), rounding=ROUND_HALF_UP)
print(f"Decimal模塊: {rounded_value_decimal}")
# 使用numpy庫
rounded_value_numpy = np.round(value, 2)
print(f"numpy庫: {rounded_value_numpy}")
截圖如下:

到此這篇關(guān)于python保留小數(shù)點位數(shù)的多種方式(附demo)的文章就介紹到這了,更多相關(guān)python保留小數(shù)點位數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- python 正確保留多位小數(shù)的實例
- python 除法保留兩位小數(shù)點的方法
- python格式化輸出保留2位小數(shù)的實現(xiàn)方法
- python保留小數(shù)位的三種實現(xiàn)方法
- python中round函數(shù)保留兩位小數(shù)的方法
- Python如何保留float類型小數(shù)點后3位
- python保留兩位小數(shù)的3種方法實例
- python保留小數(shù)函數(shù)的幾種使用總結(jié)
- python保留若干位小數(shù)?format與round的使用區(qū)別
- Python保留指定位數(shù)小數(shù)的5種方法總結(jié)
- 如何利用Python保留指定位數(shù)的小數(shù)
- Python除法保留兩位小數(shù)點的三種方法實現(xiàn)
- python保留兩位小數(shù)的五種方法
- python實現(xiàn)保留小數(shù)位數(shù)的3種方法
相關(guān)文章
利用Python將分組文本轉(zhuǎn)為Excel的流程步驟
在英語學(xué)習(xí)過程中,我們經(jīng)常會接觸各種分組整理的詞匯表,如果你下載了一個 .txt 格式的四級詞匯表,打算分類整理后導(dǎo)入 Excel 學(xué)習(xí)軟件中,大概率你會遇到格式雜亂、分組不清,所以本文給大家介紹了如何用 Python 腳本自動讀取一份分組文本,需要的朋友可以參考下2025-07-07
Pycharm中配置遠(yuǎn)程Docker運(yùn)行環(huán)境的教程圖解
這篇文章主要介紹了Pycharm中配置遠(yuǎn)程Docker運(yùn)行環(huán)境,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06
使用APScheduler3.0.1 實現(xiàn)定時任務(wù)的方法
今天小編就為大家分享一篇使用APScheduler3.0.1 實現(xiàn)定時任務(wù)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07
解決Keras中Embedding層masking與Concatenate層不可調(diào)和的問題
這篇文章主要介紹了解決Keras中Embedding層masking與Concatenate層不可調(diào)和的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06
如何利用python將Xmind用例轉(zhuǎn)為Excel用例
這篇文章主要介紹了如何利用python將Xmind用例轉(zhuǎn)為Excel用例,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-06-06
win10下opencv-python特定版本手動安裝與pip自動安裝教程
這篇文章主要介紹了win10下opencv-python特定版本手動安裝與pip自動安裝教程,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03

