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

在 Python 中舍入一個數字但保持結尾為零

Rounding a number in Python but keeping ending zeros(在 Python 中舍入一個數字但保持結尾為零)
本文介紹了在 Python 中舍入一個數字但保持結尾為零的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我一直在編寫一個腳本,該腳本從 Excel 電子表格中獲取數據,對數字進行四舍五入并刪除小數點,例如,2606.89579999999 變為 26069.但是,我需要將數字四舍五入到小數點后兩位后面會有一個零,所以 2606.89579999999 應該變成 260690.

I've been working on a script that takes data from an Excel spreadsheet, rounds the numbers, and removes the decimal point, for example, 2606.89579999999 becomes 26069. However, I need the number to round to two decimal places even if there would be a trailing zero, so 2606.89579999999 should become 260690.

我目前有它,所以 i 從 Excel 中的單元格中獲取數據,并將其四舍五入到小數點后兩位 (i = round(i, 2))在上面的例子中給了我一個小數點.

I currently have it so i takes the data from the cell in Excel, and rounds it to two decimal places (i = round(i, 2)) which gives me the single decimal point in the above example.

我已經嘗試弄清楚如何讓它與 Decimal 一起工作,但我似乎無法讓它工作.

I've tried figuring out how to get this to work with Decimal, but I can't seem to get it working.

所有其他四舍五入的數字,如果四舍五入的值不以0"結尾,則可以使用 round(i, 2),但如果數字恰好以*.x0,那個 0 被丟棄并與數據混淆.

All other numbers that get rounded, if the rounded value doesn't end in '0', work fine with round(i, 2), but if the numbers just so happen to end in *.x0, that 0 gets dropped off and messes with the data.

推薦答案

正如你所說的尾隨零,這是一個關于表示為字符串的問題,你可以使用

As you are talking about trailing zeros, this is a question about representation as string, you can use

>>> "%.2f" % round(2606.89579999999, 2)
'2606.90'

或者使用帶有format功能的現代風格:

Or use modern style with format function:

>>> '{:.2f}'.format(round(2606.89579999999, 2))
'2606.90'

并用 replacetranslate 刪除點(_ 指 python 控制臺中先前命令的結果):

and remove point with replace or translate (_ refers to result of previous command in python console):

>>> _.translate(None, '.')
'260690'

注意這里不需要四舍五入,因為 .2f 格式應用相同的四舍五入:

Note that rounding is not needed here, as .2f format applyies the same rounding:

>>> "%.2f" % 2606.89579999999
'2606.90'

但正如您提到的 excel,您可能會選擇滾動自己的舍入函數,或使用 decimal,因為 float.round 會由于浮點表示而導致奇怪的結果:

But as you mentioned excel, you probably would opt to roll your own rounding function, or use decimal, as float.round can lead to strange results due to float representation:

>>> round(2.675, 2)
2.67
>>> round(2606.89579999999, 2)
2606.89

使用小數量化:

>>> from decimal import *
>>> x = Decimal('2606.8950000000001')
# Decimal('2606.8950000000001')
>>> '{}'.format(x.quantize(Decimal('.01'), rounding=ROUND_HALF_EVEN))
'2606.90'

對于您的原始任務,變為:

That, for your original task, becomes:

>>> x = Decimal('2606.8950000000001')
>>> int((x*100).quantize(1, rounding=ROUND_HALF_EVEN))
260690

Decimal 出現了奇怪的四舍五入的原因:

And the reason of strange rounding comes to the front with Decimal:

>>> x = Decimal(2606.8950000000001)
# Decimal('2606.89499999999998181010596454143524169921875') # internal float repr

這篇關于在 Python 中舍入一個數字但保持結尾為零的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 的意思不同)
主站蜘蛛池模板: 亚洲欧美日韩精品久久亚洲区 | 视频在线一区 | 亚洲精品在线看 | av电影手机版 | 中文字幕在线免费观看 | av在线黄| 精品熟人一区二区三区四区 | 青青草原精品99久久精品66 | 91麻豆精品国产91久久久久久 | 狠狠av | 国产精品日本一区二区在线播放 | 日韩视频精品在线 | 亚洲国产精品一区二区三区 | 国产精品久久久久久久久久久久久久 | 91中文字幕在线 | 伊人精品久久久久77777 | 看片一区| www在线| 国产亚洲精品精品国产亚洲综合 | 国产精品美女久久久久久免费 | 欧美亚洲一级 | www.夜夜草| 久久久高清 | 国产亚洲精品久久19p | 久热久热 | 蜜臀av日日欢夜夜爽一区 | 91免费版在线观看 | 伊人婷婷 | 国产日韩精品视频 | 九九久久国产 | 四虎成人精品永久免费av九九 | 亚洲在线看| 日韩高清一区 | 国产精品久久av | 男女视频91| 欧美日韩国产精品 | 国产精品日韩欧美 | 亚洲精品黄色 | 精品在线免费观看视频 | 欧美成人在线影院 | 一区福利视频 |