python 在数百后截断?
如何截断像 315.15321531321 这样的输入 我想截断百分之位后的所有值,使其变为 315.15
我该怎么做?
How can truncate an input like 315.15321531321
I want to truncate all the values after the hundredths position so it becomes 315.15
how do i do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
python 2.x 下的字符串格式化应该可以为您做到这一点:
这将字符串表示形式限制为仅 2 位小数。 请注意,如果您使用
round(315.153215, 2)
,您最终会得到另一个浮点值,这自然是不精确的(或过于精确,取决于您如何看待它):从技术上讲,
round()
是正确的,但它不会按照您在315.15
请求的方式“截断”结果。 此外,如果您对 315.157 这样的值进行舍入,它将产生更接近 315.16 的值...不确定这是否是您所说的“截断”的意思。String formatting under python 2.x should do it for you:
This limits the string representation to just 2 decimal places. Note that if you use
round(315.153215, 2)
, you'll end up with another float value, which is naturally imprecise (or overprecise, depending on how you look at it):Technically,
round()
is correct, but it doesn't "truncate" the results as you requested at315.15
. In addition, if you round a value like 315.157, it will produce something closer to 315.16... not sure if that's what you mean by "truncate".看起来 print "%.2f" 也进行了舍入。
这是舍入和截断的 Python 代码
Looks like print "%.2f" does rounding as well.
Here is Python code that rounds and truncates
如果您正在处理货币金额,我强烈建议您使用 Python 的十进制类: http:// /docs.python.org/library/decimal.html
If you're working with currency amounts, I strongly recommend that you use Python's decimal class instead: http://docs.python.org/library/decimal.html
如果您只想将其缩短显示,可以使用
"%f"
格式化标志:如果您想真正切断“附加”数字,请执行以下操作:
If you just want to display it shortened, you can use the
"%f"
formating flag:If you want to really cut off the "additional" digits, do something like:
也许这会对您有所帮助:
说明:如果您想四舍五入到小数点后第二位,那么您应该:
1. 将您的数字乘以 100; 这给你31515.321531321,
2. 将这个新数字转换为整数; int(31515.321531321) 给你 31515,
3. 将其除以 100.0; 31515/100.0,
4.然后你就得到了正确答案; 315.15
享受吧!
Maybe this will help you:
Explanation: If you want to round at the second decimal place then you should:
1. multiply your number by 100; this gives you 31515.321531321,
2. convert this new number to integer; int(31515.321531321) gives you 31515,
3. divide this by 100.0; 31515/100.0,
4. and you get the correct answer; 315.15
Enjoy!
您有多种选择 - 您可以使用 round() 对数字进行舍入,但这可能会带来一些不准确的情况(例如,315.15 可能舍入为 315.150000003)。 如果您只是想在显示浮点数时截断它的值,则可以使用 printf("%.2f", mynumber) 指定输出的宽度。 这可能是一个更好的解决方案,因为在不了解有关特定应用程序的更多信息的情况下,通常最好保留数字的整个长度以进行计算。
You have several options - you can round the number using round(), however this can introduce some inaccuracies (315.15 might round to 315.150000003 for example). If you're just looking to truncate the value of the float when you're displaying it, you can specify the width of the output using printf("%.2f", mynumber). This is probably a better solution, since without knowing more about your specific application it's a good idea in general to keep the entire length of the number for calculation.
内置函数
round()
:Built-in function
round()
:当使用
Decimal
类型和货币时,金额需要精确,以下是我想出的截断为美分(百分之一)的方法:一点历史:我尝试了量化,但它倾向于四舍五入而不是截断。 而且出于良心,我不能使用昂贵的字符串格式化程序来完成如此简单的事情。 最后,我不想出现任何精度问题,所以我使用了非常具体的转换,这可能有点过头了。
When working with
Decimal
types and currency, where the amount needs to be precise, here is what I came up with for truncating to cents (hundredths):A little history: I tried
quantize
but it tends to round rather than truncate. And I couldn't in good conscience use expensive string formatters for something so simple. And finally I didn't want any precision problems a lafloat
so I use very specific casting that is probably overkill.