当前位置:   article > 正文

ValueError: invalid literal for int() with base 10: 7.8000000e+02

ValueError: invalid literal for int() with base 10: 7.8000000e+02

ValueError: invalid literal for int{} with base 10: 7.8000000e+02

1. ValueError

Python int() 函数的输入字符串中可以包含数字字符和 +- 符号,不能包含其它非数字字符。

number_string = "7.8000000e+02"

number_value = int(number_string)
  • 1
  • 2
  • 3
/usr/bin/python2.7 /home/strong/PycharmProjects/crash_course/python_function/number_string.py
Traceback (most recent call last):
  File "/home/strong/PycharmProjects/crash_course/python_function/number_string.py", line 4, in <module>
    number_value = int(number_string)
ValueError: invalid literal for int() with base 10: '7.8000000e+02'

Process finished with exit code 1
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

2. Example 1

number_string = "7.8000000e+02"

float_value = float(number_string)

int_value1 = int(float_value)
int_value2 = int(float(number_string))

print("float_value: " + str(float_value))
print("int_value1: " + str(int_value1))
print("int_value2: " + str(int_value2))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
/usr/bin/python2.7 /home/strong/PycharmProjects/crash_course/python_function/number_string.py
float_value: 780.0
int_value1: 780
int_value2: 780

Process finished with exit code 0
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

3. Example 2

number_string1 = "+99"
number_string2 = "-99"

int_value1 = int(number_string1)
int_value2 = int(number_string2)

print("int_value1: " + str(int_value1))
print("int_value2: " + str(int_value2))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
/usr/bin/python2.7 /home/strong/PycharmProjects/crash_course/python_function/number_string.py
int_value1: 99
int_value2: -99

Process finished with exit code 0
  • 1
  • 2
  • 3
  • 4
  • 5

References

[1] Yongqiang Cheng, https://yongqiang.blog.csdn.net/

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/秋刀鱼在做梦/article/detail/911946
推荐阅读
相关标签
  

闽ICP备14008679号