python字符串与数字的转化

2023-01-31 05:01:25 字符串 数字 转化

 

字符串转化为数字

 

#coding=gbk

#字符串转化为实数

def strtonum( s , defaultValue=0):
    try:
        f = float( s )
    except:
        f = defaultValue
    return f

#字符串转化为整数

def strtoint( s , defaultValue=0):
    try:
        f = int( s )
    except:
        f = defaultValue
    return f

A = [ "123","456","8812.90","1.2345E-6","-9.1E1" ];
SM = 0
for i in range(0,len(A)):
    SM = SM + strtonum( A[i] )
print( "SM=",SM)

 

 

python字符串与数字的转化
2008-04-29 21:13

 

数字变为字符串 str()
字符串变为数字 string.atoi(s,[,base]) //base为进制基数
 

 

浮点数转换 string.atof(s)

相关文章