赞
踩
Python int()
函数的输入字符串中可以包含数字字符和 +
、-
符号,不能包含其它非数字字符。
number_string = "7.8000000e+02"
number_value = int(number_string)
/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
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))
/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
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))
/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] Yongqiang Cheng, https://yongqiang.blog.csdn.net/
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。