[英]python 2.7 string format to decimal as round function
I got a problem in string format function which I don't understand. 我遇到了字符串格式函数的问题,我不明白。 Please help to explain why and how to fix this.
请帮助解释为什么以及如何解决这个问题。 thanks.
谢谢。 ( python 2.7.3 , [GCC 4.6.3] on linux2 , ubuntu 12.04 x86 )
(linux2上的python 2.7.3,[GCC 4.6.3],ubuntu 12.04 x86)
>>> import locale
>>> locale.format("%0.{0}f".format(2), 1.135, grouping=True)
'1.14'
>>> locale.format("%0.{0}f".format(2), 1.125, grouping=True)
'1.12'
>>> ("%0.2f")%(1.135)
'1.14'
>>> ("%0.2f")%(1.125)
'1.12'
I need the format result to match the round() function 我需要格式结果来匹配round()函数
>>> round(1.135, 2)
1.14
>>> round(1.125, 2)
1.13
Thank everyone. 谢谢大家。
That's because rounding is not simply bringing the last digit greater than 5 up, while truncating those lower than 4, since this method would introduce an increase in the expected average of rounded numbers. 这是因为舍入不是简单地将最后一个数字大于5,而截断低于4的数字,因为这种方法会引入舍入数字的预期平均值的增加。
The solution is to use Bankers' Rounding , that's what you see here. 解决方案是使用Bankers'Founding ,这就是你在这里看到的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.