python 如何將帶小數(shù)的浮點(diǎn)型字符串轉(zhuǎn)換為整數(shù)
1、將整數(shù)的字符串表示形式傳遞給 int
2、將float的字符串表示形式傳遞給 float
但是,如果你將float型的字符串傳遞給int將會(huì)得到錯(cuò)誤。
>>> int(’5’)5>>> float(’5.0’)5.0>>> float(’5’)5.0>>> int(5.0)5>>> float(5)5.0>>> int(’5.0’)Traceback (most recent call last): File '<stdin>', line 1, in <module>ValueError: invalid literal for int() with base 10: ’5.0’>>> int(float(’5.0’))
補(bǔ)充:解決python 字符串浮點(diǎn)型轉(zhuǎn)整型問題
ValueError: invalid literal for int() with base 10
復(fù)原錯(cuò)誤:str_a = ’1.5’int_a = int(str_a)修正錯(cuò)誤:
# 修正方式1str_a = ’1.5’int_a = int(float(str_a))# 修正方式2str_a = ’1.5’int_a = int(eval(str_a))錯(cuò)誤原因
浮點(diǎn)型字符串無法直接轉(zhuǎn)int類型
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章:
1. Django ORM實(shí)現(xiàn)按天獲取數(shù)據(jù)去重求和例子2. jsp EL表達(dá)式詳解3. asp知識(shí)整理筆記4(問答模式)4. GIT相關(guān)-IDEA/ECLIPSE工具配置的教程詳解5. 解決ajax的delete、put方法接收不到參數(shù)的問題方法6. chat.asp聊天程序的編寫方法7. IntelliJ IDEA 統(tǒng)一設(shè)置編碼為utf-8編碼的實(shí)現(xiàn)8. XML入門的常見問題(一)9. Jsp中request的3個(gè)基礎(chǔ)實(shí)踐10. 怎樣才能用js生成xmldom對(duì)象,并且在firefox中也實(shí)現(xiàn)xml數(shù)據(jù)島?
