franniss 发表于 2010-3-13 01:57:17

将字符串数字转换为real数字

很有用~

// argument0 - string to make into real number
// return - real/integer the number

var t,i,r,err,expo,def;
expo=false def=-1    // default error value
t=string_replace_all(string_replace_all(string_lower(argument0)," ",""),"+","") r=""
for(i=1;i<=string_length(t);i+=1) {
err=true            // set error flag to true
if((string_char_at(t,i)='-')and((i=1)or(string_char_at(t,i-1)='e'))){r+='-' err=false}
if((string_char_at(t,i)='.')and(string_count('.',t)=1)){r+="." err=false}
if((string_char_at(t,i)='e')and(not expo)){r+="e" expo=true err=false}
if((ord(string_char_at(t,i))>47)and(ord(string_char_at(t,i))<58)){r+=string_char_at(t,i) err=false}
if(err){
if(string_char_at(t,i)='.'){error_last="Error: in real_ex("+t+"): Multiple decimal points." return def}
if((string_count('e',t)!=0)and(i>string_pos('e',t))){error_last="Error: in real_ex("+t+"): Decimal dot in exponent." return def}
if!((ord(string_char_at(t,i))>47)and(ord(string_char_at(t,i))<58)){error_last="Error: in real_ex("+t+"): Invalid character(s)." return def}
error_last="Error: in real_ex("+t+"): Unknown error."
return def
}
}
return real(r)
页: [1]
查看完整版本: 将字符串数字转换为real数字