在 Python 中舍入一个数字但保持以零结尾
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19986662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rounding a number in Python but keeping ending zeros
提问by Seth Koberg
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.
我一直在编写一个脚本,它从 Excel 电子表格中获取数据,对数字进行四舍五入并删除小数点,例如,2606.89579999999 变为 26069。但是,我需要将数字四舍五入到两位小数,即使有尾随零,所以 2606.89579999999 应该变成 260690。
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.
我目前拥有它,因此i
从 Excel 中的单元格中获取数据,并将其四舍五入到两个小数位 ( i = round(i, 2)
),这给了我上面示例中的单个小数点。
I've tried figuring out how to get this to work with Decimal
, but I can't seem to get it working.
我试图弄清楚如何Decimal
让它与 一起工作,但我似乎无法让它工作。
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.
所有其他四舍五入的数字,如果四舍五入的值不以“0”结尾,则使用 可以正常工作round(i, 2)
,但如果数字恰好以 *.x0 结尾,则 0 会丢失并与数据混淆。
采纳答案by alko
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'
Or use modern style with format
function:
或者使用具有format
功能的现代风格:
>>> '{:.2f}'.format(round(2606.89579999999, 2))
'2606.90'
and remove point with replace
or translate
(_
refers to result of previous command in python console):
并使用replace
或删除点translate
(_
指的是python控制台中上一个命令的结果):
>>> _.translate(None, '.')
'260690'
Note that rounding is not needed here, as .2f
format applyies the same rounding:
请注意,此处不需要舍入,因为.2f
格式适用相同的舍入:
>>> "%.2f" % 2606.89579999999
'2606.90'
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:
但是正如您提到的 excel,您可能会选择滚动自己的舍入函数,或使用decimal,因为float.round
由于浮点表示可能会导致奇怪的结果:
>>> round(2.675, 2)
2.67
>>> round(2606.89579999999, 2)
2606.89
With decimal use quantize:
使用十进制quantize:
>>> 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
And the reason of strange rounding comes to the front with Decimal
:
而奇怪的四舍五入的原因出现在前面Decimal
:
>>> x = Decimal(2606.8950000000001)
# Decimal('2606.89499999999998181010596454143524169921875') # internal float repr
回答by Maciej Gol
>>> '{:.2f}'.format(2606.89579999999).replace('.', '')
'260690'
回答by user2437648
>>> int (round (2606.89579999999,2)*100)
260690