免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 8768 | 回复: 5
打印 上一主题 下一主题

中文数字转阿拉伯数字,怎么解决 [复制链接]

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-07-26 21:40 |只看该作者 |倒序浏览
'四万三千五百二十一',
'三千五百二十',
'三千五百零八',
'三千零七 '

这样的中文数字转为阿拉数字,请教各位大大,python实现
搜了网上的一些例子,要么有bug,要么看不懂

论坛徽章:
0
2 [报告]
发表于 2010-07-27 10:48 |只看该作者
我試了一下万以內的都可以轉換.

你看看如下代碼是否可用?
  1. #coding=big5

  2. strdic={'十':10,'百':100,'千':1000,'万':10000,'零':0}
  3. num={'一':1,'二':2,'三':3,'四':4,'五':5,'六':6,'七':7,'八':8,'九':9,'零':0}

  4. def to(strnum):
  5.    first = strnum[:4]
  6.    
  7.    if first.__len__()==2:
  8.       return num[first]
  9.    elif first.__len__()==0:
  10.       return 0
  11.    return num[first[:2]]*strdic[first[2:]] + to(strnum[4:])
  12.    
  13.    
  14.    
  15. if __name__ == '__main__':
  16.    str='四万三千五百二十一'
  17.    print to(str.replace('零','零零'))
  18.    
  19.    
复制代码

论坛徽章:
0
3 [报告]
发表于 2010-07-27 10:49 |只看该作者
我的是繁體系統,Python2.5.4測試通過.

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
4 [报告]
发表于 2010-07-27 11:46 |只看该作者
str='八百零七万九千二百零八'
还是有bug
我贴一个另高人写的,目前还没发现bug
  1. #coding=gbk
  2. b = '三千五百二十三'
  3. a = '七十五亿八百零七万九千二百零八'
  4. c = '四万三千五百二十一'
  5. d = '三千五百二十一'
  6. e = '三千五百零八'


  7. dict ={'零':0,'一':1,'二':2,'三':3,'四':4,'五':5,'六':6,'七':7,'八':8,'九':9,'十':10,'百':100,'千':1000,'万':10000,'亿':100000000}
  8. def getResultForDigit(a):
  9.     count = 0
  10.     result = 0
  11.     tmp = 0
  12.     Billion = 0  
  13.    
  14.     while count < len(a):
  15.         tmpChr = a[count:count+2]
  16.         print tmpChr
  17.         tmpNum = dict[tmpChr]
  18.          #如果等于1亿
  19.         if tmpNum == 100000000:
  20.             result = result + tmp
  21.             result = result * tmpNum
  22.             #获得亿以上的数量,将其保存在中间变量Billion中并清空result
  23.             Billion = result
  24.             result = 0
  25.             tmp = 0
  26.         #如果等于1万
  27.         if tmpNum == 10000:
  28.                 result = result + tmp
  29.                 result = result * tmpNum
  30.         #如果等于十或者百,千
  31.         elif tmpNum >= 10:
  32.             result = result + tmpNum * tmp
  33.             tmp = 0
  34.         #如果是个位数
  35.         else:
  36.             tmp = tmpNum
  37.         count = count + 2
  38.     result = result + tmp
  39.     result = result + Billion
  40.     print result

  41. if __name__ =="__main__":
  42.    
  43.     getResultForDigit(e)
  44.     print len(e)
复制代码

论坛徽章:
0
5 [报告]
发表于 2010-07-27 11:54 |只看该作者
我说过只支持测试万以内的。因为我定义的最高位只到万。如果要加应该也是可以的。

论坛徽章:
0
6 [报告]
发表于 2012-11-13 11:49 |只看该作者
common_used_numerals ={'零':0,'一':1,'二':2,'三':3,'四':4,'五':5,'六':6,'七':7,'八':8,'九':9,'十':10,'百':100,'千':1000,'万':10000,'亿':100000000}
def chinese2digits(uchars_chinese):
    total = 0
    r = 1
    for i in range(len(uchars_chinese) - 1, -1, -1):
        x = common_used_numerals.get(uchars_chinese[i], 0)
        if x >= 10:
            if x > r:
                r = x
            else:
                r = r * x
        else:
            total += r * x
   
    return total
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP