欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python保留小數(shù)點(diǎn)位數(shù)的多種方式(附demo)

 更新時(shí)間:2024年06月03日 09:46:41   作者:碼農(nóng)研究僧  
在Python中,保留小數(shù)點(diǎn)后特定位數(shù)可以通過(guò)多種方式實(shí)現(xiàn),以下是幾種常見(jiàn)的方法,并附上相應(yīng)的代碼示例,使用字符串格式化,使用round()函數(shù),使用Decimal模塊和使用numpy庫(kù),文中通過(guò)代碼講解的非常詳細(xì),需要的朋友可以參考下

前言

在Python中,保留小數(shù)點(diǎn)后特定位數(shù)可以通過(guò)多種方式實(shí)現(xiàn)

以下是幾種常見(jiàn)的方法,并附上相應(yīng)的代碼示例:

  • 使用字符串格式化(String Formatting)
  • 使用round()函數(shù)
  • 使用Decimal模塊
  • 使用numpy庫(kù)

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:使用百分號(hào) (%) 格式化

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ù)點(diǎn)后的位數(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庫(kù)

大量的數(shù)值計(jì)算,使用numpy庫(kù)是個(gè)好選擇

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}")

# 使用百分號(hào) (%) 格式化
formatted_value_percent = "%.2f" % value
print(f"百分號(hào)格式化: {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庫(kù)
rounded_value_numpy = np.round(value, 2)
print(f"numpy庫(kù): {rounded_value_numpy}")

截圖如下:

到此這篇關(guān)于python保留小數(shù)點(diǎn)位數(shù)的多種方式(附demo)的文章就介紹到這了,更多相關(guān)python保留小數(shù)點(diǎn)位數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python數(shù)據(jù)可視化Seaborn畫(huà)熱力圖

    python數(shù)據(jù)可視化Seaborn畫(huà)熱力圖

    這篇文章主要介紹了數(shù)據(jù)可視化Seaborn畫(huà)熱力圖,熱力圖的想法其實(shí)很簡(jiǎn)單,用顏色替換數(shù)字,下面我們來(lái)看看文章對(duì)操作過(guò)程的具體介紹吧,需要的小伙伴可以參考一下具體內(nèi)容,希望對(duì)你有所幫助
    2022-01-01
  • python 實(shí)現(xiàn)紅包隨機(jī)生成算法的簡(jiǎn)單實(shí)例

    python 實(shí)現(xiàn)紅包隨機(jī)生成算法的簡(jiǎn)單實(shí)例

    下面小編就為大家?guī)?lái)一篇python 實(shí)現(xiàn)紅包隨機(jī)生成算法的簡(jiǎn)單實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-01-01
  • Python+PyQt5實(shí)現(xiàn)網(wǎng)口功能測(cè)試詳解

    Python+PyQt5實(shí)現(xiàn)網(wǎng)口功能測(cè)試詳解

    這篇文章主要為大家詳細(xì)介紹了Python+PyQt5實(shí)現(xiàn)網(wǎng)口功能測(cè)試的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-02-02
  • Python異常處理之常見(jiàn)異常類(lèi)型絕佳實(shí)踐詳解

    Python異常處理之常見(jiàn)異常類(lèi)型絕佳實(shí)踐詳解

    這篇文章主要為大家介紹了Python異常處理之常見(jiàn)異常類(lèi)型絕佳實(shí)踐詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09
  • Python實(shí)現(xiàn)簡(jiǎn)單http服務(wù)器

    Python實(shí)現(xiàn)簡(jiǎn)單http服務(wù)器

    這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)一個(gè)簡(jiǎn)單http服務(wù)器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • python操作MySQL數(shù)據(jù)庫(kù)的方法分享

    python操作MySQL數(shù)據(jù)庫(kù)的方法分享

    堅(jiān)持每天學(xué)一點(diǎn),每天積累一點(diǎn)點(diǎn),作為自己每天的業(yè)余收獲,這個(gè)文章是我在吃飯的期間寫(xiě)的,利用自己零散的時(shí)間學(xué)了一下python操作MYSQL,所以整理一下
    2012-05-05
  • python: 自動(dòng)安裝缺失庫(kù)文件的方法

    python: 自動(dòng)安裝缺失庫(kù)文件的方法

    今天小編就為大家分享一篇python: 自動(dòng)安裝缺失庫(kù)文件的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-10-10
  • Python3 json模塊之編碼解碼方法講解

    Python3 json模塊之編碼解碼方法講解

    這篇文章主要介紹了Python3 json模塊之編碼解碼方法講解,需要的朋友可以參考下
    2021-04-04
  • django框架如何集成celery進(jìn)行開(kāi)發(fā)

    django框架如何集成celery進(jìn)行開(kāi)發(fā)

    本文給大家詳細(xì)講解了在django框架中如何集成celery進(jìn)行開(kāi)發(fā),步驟非常詳細(xì),有需要的小伙伴可以參考下
    2017-05-05
  • 詳解如何設(shè)置Python環(huán)境變量?

    詳解如何設(shè)置Python環(huán)境變量?

    這篇文章主要介紹了如何設(shè)置Python環(huán)境變量?,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05

最新評(píng)論