網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
在進(jìn)行小數(shù)計(jì)算的時(shí)候使用float,經(jīng)常會(huì)出現(xiàn)小數(shù)位不精確的情況。在python編程中,推薦使用decimal來(lái)完成小數(shù)位的精度計(jì)算。
decimal是python中的標(biāo)準(zhǔn)庫(kù),直接將Decimal導(dǎo)入到代碼塊中使用。
decimal意思為十進(jìn)制,這個(gè)模塊提供了十進(jìn)制浮點(diǎn)運(yùn)算支持。通過(guò)幾個(gè)常見(jiàn)的實(shí)戰(zhàn)用例來(lái)說(shuō)明一下其用法。
1.?浮點(diǎn)數(shù)轉(zhuǎn)Decimal
使用Decimal.from_float函數(shù)將隨便一個(gè)float類型的小數(shù)轉(zhuǎn)換成Decimal的數(shù)據(jù)類型,結(jié)果float類型數(shù)據(jù)就顯出原形了。
#?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('浮點(diǎn)數(shù)轉(zhuǎn)為Decimal后:{0}'.format(decimal_))
#?浮點(diǎn)數(shù)轉(zhuǎn)為Decimal后:10.2449999999999992184029906638897955417633056640625
從結(jié)果來(lái)看,float浮點(diǎn)數(shù)轉(zhuǎn)換完成以后精度位數(shù)就變得很長(zhǎng)不是原先的三位小數(shù)了,這是因?yàn)閒loat浮點(diǎn)數(shù)本身就不精確轉(zhuǎn)換之后才會(huì)出現(xiàn)上面的效果。
2.?Decimal除法設(shè)置
隨機(jī)選兩個(gè)整數(shù)將其定義為Decimal類型的數(shù)據(jù),之后對(duì)這兩個(gè)整個(gè)做除法通過(guò)保留相應(yīng)的結(jié)果位數(shù)對(duì)其返回結(jié)果進(jìn)行優(yōu)化。
#?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位小數(shù)后的結(jié)果是:{0}'.format(decimal_))
#?精確到8位小數(shù)后的結(jié)果是:0.16666667
很明顯做除法以后的結(jié)果應(yīng)該是一個(gè)無(wú)限小數(shù),設(shè)置保留8位小數(shù)之后自動(dòng)進(jìn)行了四舍五入的計(jì)算得到0.16666667的結(jié)果。
3. Quantize設(shè)置結(jié)果
同樣是保留了兩位小數(shù),使用quantize函數(shù)能完成同樣的效果,默認(rèn)結(jié)果也是經(jīng)過(guò)了四舍五入的計(jì)算,若是想要固定小數(shù)位數(shù)使用此方法比較靠譜。
#?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設(shè)置保留兩位小數(shù)后的結(jié)果:{0}'.format(decimal_))
# quantize設(shè)置保留兩位小數(shù)后的結(jié)果:3.78
4.?Decimal精度設(shè)置
這里還是做一個(gè)結(jié)果為無(wú)限小數(shù)的除法,分別使用向上取整、向下取整的方式保留一定位數(shù)的小數(shù)來(lái)說(shuō)明問(wèn)題。
一般情況下可能使用的都是向上取整,但是在一些領(lǐng)域比較金融、證券行業(yè)就必須采取向下取整的方式,首先來(lái)看一下常用的向上取整的方式來(lái)保留小數(shù)。
#?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位小數(shù):{0}'.format(decimal_.quantize(Decimal('0.0000000000'))))
#?向上取整保留10位小數(shù):0.1111111112
這里有個(gè)問(wèn)題就是,如果getcontext().prec已經(jīng)設(shè)置小數(shù)位是10,那么在使用quantize函數(shù)固定小數(shù)位的時(shí)候就必須不超過(guò)10位才行,也就是不能超過(guò)有效位數(shù)否則就會(huì)報(bào)錯(cuò)。
接下來(lái)看一下向下取整,向下取整的小數(shù)保留方式只需要修改getcontext().rounding的屬性為向下取整即可,為了對(duì)比結(jié)果我們還是采用同樣的數(shù)據(jù)來(lái)看看效果。
#?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位小數(shù):{0}'.format(decimal_.quantize(Decimal('0.0000000000'))))
#?向下取整保留10位小數(shù):0.1111111111
可以發(fā)現(xiàn)同樣的數(shù)據(jù)做除法,向下取整時(shí)結(jié)果是0.1111111111,向上取整時(shí)結(jié)果是0.1111111112。
同樣,還是很多的其他保留小數(shù)的方式可以使用比如四舍五入的方式,這也是很多很多行業(yè)會(huì)采取的一種小數(shù)保留的方式,再演示一下四舍五入時(shí)的保留小數(shù)。
#?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位小數(shù):{0}'.format(decimal_.quantize(Decimal('0.0000'))))
#?四舍五入保留4位小數(shù):3.1416
將3.14159通過(guò)四舍五入的方式保留4位小數(shù)之后就變成了3.1416,和我們預(yù)想的結(jié)果一樣。
原文鏈接:https://mp.weixin.qq.com/s/P1wapmw96-aFG1iULR9VlA
相關(guān)推薦
- 2022-05-29 ASP.NET?Core使用HttpClient調(diào)用WebService_實(shí)用技巧
- 2022-06-06 Postgresql split_part()函數(shù),根據(jù)符號(hào)切割字符串
- 2023-01-29 python?pandas?解析(讀取、寫入)CSV?文件的操作方法_python
- 2022-10-19 React?Hook實(shí)現(xiàn)對(duì)話框組件_React
- 2022-11-14 深度強(qiáng)化學(xué)習(xí)預(yù)訓(xùn)練,在線、離線
- 2023-01-05 Kotlin啟動(dòng)協(xié)程的三種方式示例詳解_Android
- 2023-02-01 Python?AI編程助手AICodeHelper使用示例_python
- 2022-07-19 安卓TextView的lineHeight*lineCount!=height問(wèn)題,解決不支持滾動(dòng)的
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支