四舍五入python数字,但保持结束零 [英] Rounding a number in python but keeping ending zeros

查看:218
本文介绍了四舍五入python数字,但保持结束零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究一个脚本,它从XLS电子表格中获取数据,舍入数字,并删除小数点,例如:2606.89579999999变成26069;但是,我需要的数字四舍五入到小数点后两位,即使会有一个尾随零,所以2606.89579999999应该变成260690.



我现在有这样的 i 从excel中的单元格获取数据,并将其舍入到小数点后两位( i = round(i,2))这给了我在上面的例子单一的小数点。

我试着找出如何得到这个工作与十进制,但我似乎无法得到它的工作。

如果四舍五入的值不以'0'结尾,所有其他取整数字都可以正常使用 round(i,2) 但是如果这些数字恰好以* .x0结尾,那么这个数字就会被丢弃,并且会与数据混淆在一起。

解决方案

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

或者使用格式的现代风格 '{:.2f}'。format(round(2606.89579999999,2))
'2606.90'



替换 translate _ 是指在python控制台中前一个命令的结果):

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

注意四舍五入 .2f 格式应用相同舍入:

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

但是正如你提到的excel,你可能会选择推出自己的舍入函数,或者使用小数点作为<$

 > c $ c> float.round 会导致奇怪的结果。 >> (2.675,2)
2.67
>>> (2606.89579999999,2)
2606.89

用十进制表示 quantize

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

$


$ b


$ b

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

奇怪的四舍五入的原因来自前面 Decimal

 >>> x =十进制(2606.8950000000001)
#十进制('2606.89499999999998181010596454143524169921875')#内部浮动repr


I've been working on a script that takes data from an XLS spreadsheet, rounds the numbers, and removes the decimal point, ex: 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 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've tried figuring out how to get this to work with Decimal, but I can't seem to get it working.

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'

Or use modern style with format function:

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

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

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

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

>>> "%.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:

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

With decimal use 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:

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

这篇关于四舍五入python数字,但保持结束零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆