久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Python float 到 int 的轉換

Python float to int conversion(Python float 到 int 的轉換)
本文介紹了Python float 到 int 的轉換的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

基本上,我將浮點數轉換為整數,但我并不總是有預期值.

Basically, I'm converting a float to an int, but I don't always have the expected value.

這是我正在執行的代碼:

Here's the code I'm executing:

x = 2.51

print("--------- 251.0")
y = 251.0
print(y)
print(int(y))

print("--------- 2.51 * 100")
y = x * 100
print(y)
print(int(y))

print("--------- 2.51 * 1000 / 10")
y = x * 1000 / 10
print(y)
print(int(y))

print("--------- 2.51 * 100 * 10 / 10")
y = x * 100 * 10 / 10
print(y)
print(int(y))

x = 4.02
print("--------- 402.0")
y = 402.0
print(y)
print(int(y))

print("--------- 4.02 * 100")
y = x * 100
print(y)
print(int(y))

print("--------- 4.02 * 1000 / 10")
y = x * 1000 / 10
print(y)
print(int(y))

print("--------- 4.02 * 100 * 10 / 10")
y = x * 100 * 10 / 10
print(y)
print(int(y))

這是結果(第一個值是操作的結果,第二個值是相同操作的 int()):

And here's the result (first value is the result of the operation, second value is int() of the same operation):

--------- 251.0
251.0
251
--------- 2.51 * 100
251.0
250
--------- 2.51 * 1000 / 10
251.0
251
--------- 2.51 * 100 * 10 / 10
251.0
250
--------- 402.0
402.0
402
--------- 4.02 * 100
402.0
401
--------- 4.02 * 1000 / 10
402.0
401
--------- 4.02 * 100 * 10 / 10
402.0
401

2.51 和 4.02 是導致 2.50 -> 5.00 范圍內奇怪行為的唯一值.當給定相同的操作時,該范圍內的每隔兩個數字值轉換為 int 沒有任何問題.

2.51 and 4.02 are the only values that lead to that strange behaviour on the 2.50 -> 5.00 range. Every other two digits value in that range converts to int without any problem when given the same operations.

那么,我錯過了什么導致這些結果?順便說一下,我使用的是 Python 2.7.2.

So, what am I missing that leads to those results? I'm using Python 2.7.2 by the way.

推薦答案

2.51 * 100 = 250.999999999997

int() 函數只是截斷小數點處的數字,得到 250.使用

The int() function simply truncates the number at the decimal point, giving 250. Use

int(round(2.51*100)) 

獲取 251 作為整數.一般來說,浮點數不能精確表示.因此,應注意舍入誤差.如前所述,這不是 Python 特有的問題.這是所有計算機語言中反復出現的問題.

to get 251 as an integer. In general, floating point numbers cannot be represented exactly. One should therefore be careful of round-off errors. As mentioned, this is not a Python-specific problem. It's a recurring problem in all computer languages.

這篇關于Python float 到 int 的轉換的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Python 3 Float Decimal Points/Precision(Python 3 浮點小數點/精度)
Converting Float to Dollars and Cents(將浮點數轉換為美元和美分)
What are some possible calculations with numpy or scipy that can return a NaN?(numpy 或 scipy 有哪些可能的計算可以返回 NaN?)
Python float to ratio(Python浮動比率)
How to manage division of huge numbers in Python?(如何在 Python 中管理大量數字的除法?)
mean from pandas and numpy differ(pandas 和 numpy 的意思不同)
主站蜘蛛池模板: 91av在线影院| 夜夜爽99久久国产综合精品女不卡 | 成人欧美一区二区三区黑人孕妇 | 婷婷五月色综合 | 国产精品久久久久无码av | 精品视频在线播放 | 亚洲欧洲成人在线 | 久久美国| 成人在线视频观看 | 一区二区三区视频播放 | 欧美视频一区二区三区 | 天天澡天天狠天天天做 | 激情91 | 亚洲精品一区二三区不卡 | 天堂久| 少妇一级淫片免费放播放 | 亚洲成人999 | 91免费观看国产 | 国产精品一区二区三区在线 | 亚洲欧美日韩在线一区二区 | 欧洲视频一区 | 欧美激情国产日韩精品一区18 | 综合久久国产 | 中文字幕一区二区视频 | 一级黄色绿像片 | 日日日干干干 | 色婷婷久久久久swag精品 | 成人在线免费视频观看 | 亚洲一区国产 | 国产第一页在线观看 | 久久久久国产一区二区三区 | 成人免费大片黄在线播放 | 日韩精品一区在线观看 | 玖玖视频免费 | 国产精品久久久久久福利一牛影视 | av激情在线 | 国产一区视频在线 | 国产精久久久久久 | 久久成人精品视频 | 91精品国产91 | 毛片在线看看 |