網站首頁 編程語言 正文
在進行小數計算的時候使用float,經常會出現小數位不精確的情況。在python編程中,推薦使用decimal來完成小數位的精度計算。
decimal是python中的標準庫,直接將Decimal導入到代碼塊中使用。
decimal意思為十進制,這個模塊提供了十進制浮點運算支持。通過幾個常見的實戰用例來說明一下其用法。
1.?浮點數轉Decimal
使用Decimal.from_float函數將隨便一個float類型的小數轉換成Decimal的數據類型,結果float類型數據就顯出原形了。
#?It?imports?the?Decimal?class?from?the?decimal?module.
import?decimal
from?decimal?import?Decimal
#?It?converts?the?float?10.245?to?a?Decimal?object.
decimal_?=?Decimal.from_float(10.245)
print('浮點數轉為Decimal后:{0}'.format(decimal_))
#?浮點數轉為Decimal后:10.2449999999999992184029906638897955417633056640625
從結果來看,float浮點數轉換完成以后精度位數就變得很長不是原先的三位小數了,這是因為float浮點數本身就不精確轉換之后才會出現上面的效果。
2.?Decimal除法設置
隨機選兩個整數將其定義為Decimal類型的數據,之后對這兩個整個做除法通過保留相應的結果位數對其返回結果進行優化。
#?It?imports?all?the?names?from?the?decimal?module?into?the?current?namespace.
from?decimal?import?*
#?It?sets?the?precision?of?the?decimal?module?to?8.
getcontext().prec?=?8
#?Dividing?1?by?6?and?storing?the?result?in?the?variable?`decimal_`.
decimal_?=?Decimal(1)/Decimal(6)
print('精確到8位小數后的結果是:{0}'.format(decimal_))
#?精確到8位小數后的結果是:0.16666667
很明顯做除法以后的結果應該是一個無限小數,設置保留8位小數之后自動進行了四舍五入的計算得到0.16666667的結果。
3. Quantize設置結果
同樣是保留了兩位小數,使用quantize函數能完成同樣的效果,默認結果也是經過了四舍五入的計算,若是想要固定小數位數使用此方法比較靠譜。
#?It?imports?all?the?names?from?the?decimal?module?into?the?current?namespace.
from?decimal?import?*
#?Rounding?the?number?3.7829?to?two?decimal?places.
decimal_?=?Decimal('3.7829').quantize(Decimal('0.00'))
print('quantize設置保留兩位小數后的結果:{0}'.format(decimal_))
# quantize設置保留兩位小數后的結果:3.78
4.?Decimal精度設置
這里還是做一個結果為無限小數的除法,分別使用向上取整、向下取整的方式保留一定位數的小數來說明問題。
一般情況下可能使用的都是向上取整,但是在一些領域比較金融、證券行業就必須采取向下取整的方式,首先來看一下常用的向上取整的方式來保留小數。
#?It?imports?all?the?names?from?the?decimal?module?into?the?current?namespace.
from?decimal?import?*
#?It?sets?the?rounding?mode?to?ROUND_CEILING.
getcontext().rounding?=?getattr(decimal,?'ROUND_CEILING')
#?It?sets?the?precision?of?the?decimal?module?to?10.
getcontext().prec?=?10
#?Converting?the?integer?9?to?a?string?and?then?converting?it?to?a?Decimal?object.
decimal_?=?Decimal(1)?/?Decimal(str(9))
print('向上取整保留10位小數:{0}'.format(decimal_.quantize(Decimal('0.0000000000'))))
#?向上取整保留10位小數:0.1111111112
這里有個問題就是,如果getcontext().prec已經設置小數位是10,那么在使用quantize函數固定小數位的時候就必須不超過10位才行,也就是不能超過有效位數否則就會報錯。
接下來看一下向下取整,向下取整的小數保留方式只需要修改getcontext().rounding的屬性為向下取整即可,為了對比結果我們還是采用同樣的數據來看看效果。
#?It?imports?all?the?names?from?the?decimal?module?into?the?current?namespace.
from?decimal?import?*
#?It?sets?the?rounding?mode?to?ROUND_CEILING.
getcontext().rounding?=?getattr(decimal,?'ROUND_FLOOR')
#?It?sets?the?precision?of?the?decimal?module?to?10.
getcontext().prec?=?10
#?Converting?the?integer?9?to?a?string?and?then?converting?it?to?a?Decimal?object.
decimal_?=?Decimal(1)?/?Decimal(str(9))
print('向下取整保留10位小數:{0}'.format(decimal_.quantize(Decimal('0.0000000000'))))
#?向下取整保留10位小數:0.1111111111
可以發現同樣的數據做除法,向下取整時結果是0.1111111111,向上取整時結果是0.1111111112。
同樣,還是很多的其他保留小數的方式可以使用比如四舍五入的方式,這也是很多很多行業會采取的一種小數保留的方式,再演示一下四舍五入時的保留小數。
#?It?imports?all?the?names?from?the?decimal?module?into?the?current?namespace.
from?decimal?import?*
#?It?sets?the?rounding?mode?to?ROUND_HALF_UP.
getcontext().rounding?=?getattr(decimal,?'ROUND_HALF_UP')
#?It?sets?the?precision?of?the?decimal?module?to?4.
getcontext().prec?=?5
#?It?converts?the?string?'3.14159'?to?a?Decimal?object.
decimal_?=?Decimal('3.14159')
print('四舍五入保留4位小數:{0}'.format(decimal_.quantize(Decimal('0.0000'))))
#?四舍五入保留4位小數:3.1416
將3.14159通過四舍五入的方式保留4位小數之后就變成了3.1416,和我們預想的結果一樣。
原文鏈接:https://mp.weixin.qq.com/s/P1wapmw96-aFG1iULR9VlA
相關推薦
- 2023-07-02 Pandas數據查詢的集中實現方法_python
- 2022-07-19 Docker容器內存占用過高解決方法
- 2022-03-14 has been blocked by CORS policy: Response to prefl
- 2022-02-14 Uncaught TypeError: Failed to execute ‘appendChild
- 2022-10-31 總結Golang四種不同的參數配置方式_Golang
- 2022-09-10 詳解Python腳本如何設置試用期_python
- 2022-02-22 Element-UI二次封裝實現TreeSelect 樹形下拉選擇組件
- 2022-12-14 React懶加載實現原理深入分析_React
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支