像以下類似的帖子不回答我的問題。 Convert a string to integer with decimal in Python如何在Python中將字符串轉換爲十進制數字進行算術運算?
考慮以下Python代碼。
>>> import decimal
>>> s = '23.456'
>>> d = decimal.Decimal(s)
>>> d
Decimal('23.456') # How do I represent this as simply 23.456?
>>> d - 1
22 # How do I obtain the output to be 22.456?
如何將字符串轉換爲十進制數,以便我能夠對其執行算術運算並獲得正確精度的輸出?
看起來像上一個問題。使用float()。 http://stackoverflow.com/questions/482410/how-do-i-convert-a-string-to-a-double-in-python –
這有什麼錯'X =浮動(23.456) - 1'。 – RanRag
http:// ideone。com/TA3js – wroniasty