幻想森林

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 2129|回复: 0

[脚本] 将字符串数字转换为real数字

[复制链接]

335

主题

782

帖子

1万

积分

版主

Rank: 7Rank: 7Rank: 7

积分
12742
发表于 2010-3-13 01:57:17 | 显示全部楼层 |阅读模式
很有用~
  1. // argument0 - string to make into real number
  2. // return - real/integer the number
  3. var t,i,r,err,expo,def;
  4. expo=false def=-1    // default error value
  5. t=string_replace_all(string_replace_all(string_lower(argument0)," ",""),"+","") r=""
  6. for(i=1;i<=string_length(t);i+=1) {
  7. err=true            // set error flag to true
  8. if((string_char_at(t,i)='-')and((i=1)or(string_char_at(t,i-1)='e'))){r+='-' err=false}
  9. if((string_char_at(t,i)='.')and(string_count('.',t)=1)){r+="." err=false}
  10. if((string_char_at(t,i)='e')and(not expo)){r+="e" expo=true err=false}
  11. if((ord(string_char_at(t,i))>47)and(ord(string_char_at(t,i))<58)){r+=string_char_at(t,i) err=false}
  12. if(err){
  13. if(string_char_at(t,i)='.'){error_last="Error: in real_ex("+t+"): Multiple decimal points." return def}
  14. 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}
  15. 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}
  16. error_last="Error: in real_ex("+t+"): Unknown error."
  17. return def
  18. }
  19. }
  20. return real(r)
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|幻想森林

GMT+8, 2024-4-20 03:03 , Processed in 0.017787 second(s), 20 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表