在Python中将字符串转换为带小数的整数

发布于 2024-07-26 03:58:49 字数 523 浏览 5 评论 0原文

我有一个格式为“nn.nnnnn”的字符串,我想将其转换为整数。

直接转换失败:

>>> s = '23.45678'
>>> i = int(s)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '23.45678'

我可以使用以下方法将其转换为小数:

>>> from decimal import *
>>> d = Decimal(s)
>>> print d
23.45678

我还可以按“.”进行拆分,然后从零中减去小数,然后将其添加到整数中......恶心。

但我更喜欢将其作为 int 类型,而不进行不必要的类型转换或操作。

I have a string in the format: 'nn.nnnnn' in Python, and I'd like to convert it to an integer.

Direct conversion fails:

>>> s = '23.45678'
>>> i = int(s)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '23.45678'

I can convert it to a decimal by using:

>>> from decimal import *
>>> d = Decimal(s)
>>> print d
23.45678

I could also split on '.', then subtract the decimal from zero, then add that to the whole number ... yuck.

But I'd prefer to have it as an int, without unnecessary type conversions or maneuvering.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(8

记忆之渊 2024-08-02 03:58:49

这个怎么样?

>>> s = '23.45678'
>>> int(float(s))
23

或者...

>>> int(Decimal(s))
23

或者...

>>> int(s.split('.')[0])
23

我怀疑事情会变得比这简单得多,恐怕。 接受它并继续前进。

How about this?

>>> s = '23.45678'
>>> int(float(s))
23

Or...

>>> int(Decimal(s))
23

Or...

>>> int(s.split('.')[0])
23

I doubt it's going to get much simpler than that, I'm afraid. Just accept it and move on.

皇甫轩 2024-08-02 03:58:49

您想要什么样的舍入行为? 你把 2.67 变成 3 或 2。如果你想使用四舍五入,试试这个:

s = '234.67'
i = int(round(float(s)))

否则,就这样做:

s = '234.67'
i = int(float(s))

What sort of rounding behavior do you want? Do you 2.67 to turn into 3, or 2. If you want to use rounding, try this:

s = '234.67'
i = int(round(float(s)))

Otherwise, just do:

s = '234.67'
i = int(float(s))
情深缘浅 2024-08-02 03:58:49
>>> s = '23.45678'
>>> int(float(s))
23
>>> int(round(float(s)))
23
>>> s = '23.54678'
>>> int(float(s))
23
>>> int(round(float(s)))
24

您没有指定是否要四舍五入...

>>> s = '23.45678'
>>> int(float(s))
23
>>> int(round(float(s)))
23
>>> s = '23.54678'
>>> int(float(s))
23
>>> int(round(float(s)))
24

You don't specify if you want rounding or not...

太阳公公是暖光 2024-08-02 03:58:49

你可以使用:

s = '23.245678'
i = int(float(s))

You could use:

s = '23.245678'
i = int(float(s))
空袭的梦i 2024-08-02 03:58:49

仅当您从一种数据类型更改为另一种数据类型且不损失保真度时,“转换”才有意义。 字符串表示的数字是浮点数,在强制转换为 int 时会失去精度。

您可能想要四舍五入(我希望这些数字不代表货币,因为四舍五入会变得更加复杂)。

round(float('23.45678'))

"Convert" only makes sense when you change from one data type to another without loss of fidelity. The number represented by the string is a float and will lose precision upon being forced into an int.

You want to round instead, probably (I hope that the numbers don't represent currency because then rounding gets a whole lot more complicated).

round(float('23.45678'))
坠似风落 2024-08-02 03:58:49

如果你想截断值,其他人提到的表达式 int(float(s)) 是最好的。 如果您想要舍入,请使用 int(round(float(s)) 如果舍入算法与您想要的相匹配(请参阅 舍入文档),否则您应该使用 Decimal 和 1(如果是舍入算法)。

The expression int(float(s)) mentioned by others is the best if you want to truncate the value. If you want rounding, using int(round(float(s)) if the round algorithm matches what you want (see the round documentation), otherwise you should use Decimal and one if its rounding algorithms.

挽袖吟 2024-08-02 03:58:49
round(float("123.789"))

会给你一个整数值,但是一个浮点类型。 然而,对于 Python 的鸭子类型,实际类型通常不是很相关。 这也会对值进行四舍五入,这可能是您不想要的。 将“round”替换为“int”,您将得到它的截断值和实际的 int。 像这样:

int(float("123.789"))

但是,再次强调,实际的“类型”通常并不那么重要。

round(float("123.789"))

will give you an integer value, but a float type. With Python's duck typing, however, the actual type is usually not very relevant. This will also round the value, which you might not want. Replace 'round' with 'int' and you'll have it just truncated and an actual int. Like this:

int(float("123.789"))

But, again, actual 'type' is usually not that important.

沉溺在你眼里的海 2024-08-02 03:58:49

我相信这是一个无用的错误,应该在 Python 中纠正。

int('2') --> 2 将字符串 '2' 转换为整数 2。

int(2.7) --> 2 将 float 转换为 int。

int('2.7') 应转换为 2。例如,这就是 Perl 的工作原理。 是的,这同时做了两件事。 它转换字符串,当它发现它是浮点表示形式时,它应该转换为 int。

否则,为什么坚持 float('2') 应该起作用呢? 它是一个整数字符串,因为没有小数点。 因此它必须从整数字符串直接转换为浮点数。

我不知道,但也许有人可以回答python解释器是否使用了所需的int(float(x)),它是否实际上经历了先转换为float然后再转换的过程至 int。 这将使这个错误变得更加需要纠正。

I believe this is a useless bug that should be corrected in Python.

int('2') --> 2 That converts the string '2' into an the integer 2.

int(2.7) --> 2 Converts a float to an int.

int('2.7') SHOULD convert to 2. This is how Perl works, for example. Yes, this does two things at once. It converts the string and when it finds it is in a representation that is a float, it should convert to int.

Otherwise, why insist that float('2') should work? It is an integer string, because there is no decimal point. So it has to convert from string which is an integer, directly to float.

I don't know but perhaps someone can answer whether the python interpreter, if the required int(float(x)) is used, if it actually goes through the process of first converting to float and then converting to int. That would make this bug even more critical to correct.

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