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

在 Python 中舍入一個(gè)數(shù)字但保持結(jié)尾為零

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

問題描述

我一直在編寫一個(gè)腳本,該腳本從 Excel 電子表格中獲取數(shù)據(jù),對數(shù)字進(jìn)行四舍五入并刪除小數(shù)點(diǎn),例如,2606.89579999999 變?yōu)?26069.但是,我需要將數(shù)字四舍五入到小數(shù)點(diǎn)后兩位后面會(huì)有一個(gè)零,所以 2606.89579999999 應(yīng)該變成 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 中的單元格中獲取數(shù)據(jù),并將其四舍五入到小數(shù)點(diǎn)后兩位 (i = round(i, 2))在上面的例子中給了我一個(gè)小數(shù)點(diǎn).

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.

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

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

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

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.

推薦答案

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

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功能的現(xiàn)代風(fēng)格:

Or use modern style with format function:

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

并用 replacetranslate 刪除點(diǎn)(_ 指 python 控制臺(tái)中先前命令的結(jié)果):

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

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

注意這里不需要四舍五入,因?yàn)?.2f 格式應(yīng)用相同的四舍五入:

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

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

但正如您提到的 excel,您可能會(huì)選擇滾動(dòng)自己的舍入函數(shù),或使用 decimal,因?yàn)?float.round 會(huì)由于浮點(diǎn)表示而導(dǎo)致奇怪的結(jié)果:

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

使用小數(shù)量化:

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

對于您的原始任務(wù),變?yōu)?

That, for your original task, becomes:

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

Decimal 出現(xiàn)了奇怪的四舍五入的原因:

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

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

這篇關(guān)于在 Python 中舍入一個(gè)數(shù)字但保持結(jié)尾為零的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

Python 3 Float Decimal Points/Precision(Python 3 浮點(diǎn)小數(shù)點(diǎn)/精度)
Converting Float to Dollars and Cents(將浮點(diǎn)數(shù)轉(zhuǎn)換為美元和美分)
What are some possible calculations with numpy or scipy that can return a NaN?(numpy 或 scipy 有哪些可能的計(jì)算可以返回 NaN?)
Python float to ratio(Python浮動(dòng)比率)
How to manage division of huge numbers in Python?(如何在 Python 中管理大量數(shù)字的除法?)
mean from pandas and numpy differ(pandas 和 numpy 的意思不同)
主站蜘蛛池模板: 91视频.com| 欧洲毛片| 日本不卡高字幕在线2019 | 久久精品女人天堂av | 中文字幕久久精品 | 免费福利视频一区二区三区 | 99精品在线 | 欧美一级淫片免费视频黄 | 亚洲精品大全 | 国产中文字幕av | 亚洲欧美在线一区 | 视频在线一区二区 | 97综合在线 | 久久免费大片 | 在线观看黄免费 | 日韩一区二区三区在线观看 | 欧美jizzhd精品欧美巨大免费 | 亚洲国产福利视频 | 亚洲欧美在线免费观看 | 亚洲一一在线 | 在线观看成年人视频 | 国产农村妇女毛片精品久久麻豆 | 九色porny自拍视频 | 国产小网站 | 人人干人人看 | 中文字幕一区二区三区不卡 | 成人亚洲一区 | 久久久国产一区二区三区 | 精品视频一区二区 | 国产欧美日韩综合精品一 | 久久中文视频 | 成人不卡一区二区 | 丝袜 亚洲 欧美 日韩 综合 | www.国产一区 | 亚洲视频免费播放 | 国产成人精品综合 | 日韩免费在线 | 色吧久久 | 色综合99| 国产一级一片免费播放 | 成人免费观看视频 |