python 在数百后截断?

发布于 2024-07-23 20:50:35 字数 78 浏览 7 评论 0原文

如何截断像 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

墨小沫ゞ 2024-07-30 20:50:35

python 2.x 下的字符串格式化应该可以为您做到这一点:

>>> print '%.2f' % 315.15321531321
315.15

这将字符串表示形式限制为仅 2 位小数。 请注意,如果您使用round(315.153215, 2),您最终会得到另一个浮点值,这自然是不精确的(或过于精确,取决于您如何看待它):

>>> round(315.15321531321, 2)
315.14999999999998

从技术上讲,round() 是正确的,但它不会按照您在 315.15 请求的方式“截断”结果。 此外,如果您对 315.157 这样的值进行舍入,它将产生更接近 315.16 的值...不确定这是否是您所说的“截断”的意思。

String formatting under python 2.x should do it for you:

>>> print '%.2f' % 315.15321531321
315.15

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):

>>> round(315.15321531321, 2)
315.14999999999998

Technically, round() is correct, but it doesn't "truncate" the results as you requested at 315.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".

情深缘浅 2024-07-30 20:50:35

看起来 print "%.2f" 也进行了舍入。
这是舍入和截断的 Python 代码

num = 315.15627
print("rounded   = %.2f" % num)
print("truncated = %.2f" % (int(num*100)/float(100)))

print(f'rounded   = {num:.2f}')
print(f'truncated = {int(num*100)/float(100):.2f}')

**Outputs**
rounded   = 315.16
truncated = 315.15
rounded   = 315.16
truncated = 315.15

Looks like print "%.2f" does rounding as well.
Here is Python code that rounds and truncates

num = 315.15627
print("rounded   = %.2f" % num)
print("truncated = %.2f" % (int(num*100)/float(100)))

print(f'rounded   = {num:.2f}')
print(f'truncated = {int(num*100)/float(100):.2f}')

**Outputs**
rounded   = 315.16
truncated = 315.15
rounded   = 315.16
truncated = 315.15
本王不退位尔等都是臣 2024-07-30 20:50:35

如果您正在处理货币金额,我强烈建议您使用 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

戏蝶舞 2024-07-30 20:50:35

如果您只想将其缩短显示,可以使用 "%f" 格式化标志:

value = 315.123123123
print "Value is: %.2f" % value

如果您想真正切断“附加”数字,请执行以下操作:

from math import trunc
value = trunc(value*100)/100

If you just want to display it shortened, you can use the "%f" formating flag:

value = 315.123123123
print "Value is: %.2f" % value

If you want to really cut off the "additional" digits, do something like:

from math import trunc
value = trunc(value*100)/100
逆蝶 2024-07-30 20:50:35

也许这会对您有所帮助:

a=315.15321531321
a=a*100
a=int(a)
a=a/100.0
print(a)
315.15

说明:如果您想四舍五入到小数点后第二位,那么您应该:
1. 将您的数字乘以 100; 这给你31515.321531321,
2. 将这个新数字转换为整数; int(31515.321531321) 给你 31515,
3. 将其除以 100.0; 31515/100.0,
4.然后你就得到了正确答案; 315.15

享受吧!

Maybe this will help you:

a=315.15321531321
a=a*100
a=int(a)
a=a/100.0
print(a)
315.15

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!

贵在坚持 2024-07-30 20:50:35

您有多种选择 - 您可以使用 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.

请恋爱 2024-07-30 20:50:35

内置函数round()

>>> num = 315.1532153132
>>> round(num, 2)
3.1499999999999998

Built-in function round():

>>> num = 315.1532153132
>>> round(num, 2)
3.1499999999999998
江南烟雨〆相思醉 2024-07-30 20:50:35

当使用 Decimal 类型和货币时,金额需要精确,以下是我想出的截断为美分(百分之一)的方法:

amount = Decimal(long(amount * 100)) / Decimal(100)

一点历史:我尝试了量化,但它倾向于四舍五入而不是截断。 而且出于良心,我不能使用昂贵的字符串格式化程序来完成如此简单的事情。 最后,我不想出现任何精度问题,所以我使用了非常具体的转换,这可能有点过头了。

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):

amount = Decimal(long(amount * 100)) / Decimal(100)

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 la float so I use very specific casting that is probably overkill.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文