在Python中将字符串转换为整数,并使用十进制。

时间:2022-09-15 11:49:24

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

我有一个格式的字符串:nn。在Python中,我想把它转换成整数。

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.

我也可以分开。,然后从0中减去小数点,然后把它加到整个数字中。讨厌的东西。

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

但我更希望它是一个int型的,没有不必要的类型转换或机动。

7 个解决方案

#1


90  

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.

恐怕要比这简单得多。接受它,继续前进。

#2


13  

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:

你想要什么样的舍入行为?你是2。67变成3,还是2。如果你想用四舍五入,试试这个:

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

Otherwise, just do:

否则,只做:

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

#3


3  

>>> 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...

你没有指定是否要四舍五入…

#4


2  

"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'))

#5


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.

如果您想要截断这个值,那么其他人提到的表达式int(float(s))是最好的。如果你想四舍五入,使用整数(圆(浮点数)),如果圆算法与你想要的匹配(请参阅圆形文档),否则你应该使用十进制和一个如果它的四舍五入算法。

#6


1  

You could use:

您可以使用:

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

#7


0  

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:

将给您一个整数值,但是是浮点类型。但是,对于Python的duck类型,实际类型通常并不十分相关。这也会绕过你可能不想要的值。用“int”替换“round”,你就会把它截断,变成一个实际的整数。

int(float("123.789"))

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

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

#1


90  

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.

恐怕要比这简单得多。接受它,继续前进。

#2


13  

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:

你想要什么样的舍入行为?你是2。67变成3,还是2。如果你想用四舍五入,试试这个:

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

Otherwise, just do:

否则,只做:

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

#3


3  

>>> 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...

你没有指定是否要四舍五入…

#4


2  

"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'))

#5


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.

如果您想要截断这个值,那么其他人提到的表达式int(float(s))是最好的。如果你想四舍五入,使用整数(圆(浮点数)),如果圆算法与你想要的匹配(请参阅圆形文档),否则你应该使用十进制和一个如果它的四舍五入算法。

#6


1  

You could use:

您可以使用:

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

#7


0  

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:

将给您一个整数值,但是是浮点类型。但是,对于Python的duck类型,实际类型通常并不十分相关。这也会绕过你可能不想要的值。用“int”替换“round”,你就会把它截断,变成一个实际的整数。

int(float("123.789"))

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

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