通用数字中文互转换工具代码

问题描述

今天在CodeProject首次发表文章:http://www.codeproject.com/KB/cs/NumberToFromWord.aspx,这里特别对中文数字转换做一说明。这里好像不能上传,欢迎大家到我的博客下载代码http://www.cnblogs.com/cruisoring/archive/2011/11/21/2257091.html作为一个可扩展的工具,它能够实现数字转中文,以及中文转数字。大体思路是为每个数字建立建立字典Dictionary<int,List<string>>NumberNameDict,即newDictionary<int,List<string>>{{10,newList<string>{"十","拾"}},{20,newList<string>{"廿"}},{30,newList<string>{"卅"}},{100,newList<string>{"百","佰"}},{1000,newList<string>{"千","仟"}},{10000,newList<string>{"万"}},{100000000,newList<string>{"亿"}}},newList<string>(){"○一二三四五六七八九","0123456789","零壹贰叁肆伍陆柒捌玖"}存储所有对应的中文,其中List<string>仅对中文有效,存储0-9对应的中文字符,其中第一个字符串为缺省输出格式。类的初始化程序由此建立反向查询的字典Dictionary<string,int>WordNameDict:foreach(KeyValuePair<int,List<string>>kvpinNumberNameDict){foreach(stringsinkvp.Value){WordNameDict.Add(s,kvp.Key);if(!AllWords.Contains(s))AllWords.Add(s);}}另外对于进制数建立列表protectedList<int>groupNums:groupNums=newList<int>();intmax=NumberNameDict.Keys.Max();for(inti=firstGroupNum;i<=max;i*=10){if(NumberNameDict.ContainsKey(i)){//GroupNameDict.Add(i,NumberNameDict[i][0]);groupNums.Add(i);}}groupNums.Sort();结果是{100000000,10000,1000,100,10},即"亿万千百十"对应的数字列表。数字转中文为实现数字转中文,首先对于每个非零的字符进行转换,其位数按照groupNums解析:List<string>sections=newList<string>();intremained=number;for(inti=0;i<groupNums.Count;i++){if(remained<groupNums[i])continue;intwhole=remained/groupNums[i];sections.Add(toWords(whole));if(ToPlural!=null&&whole!=1)sections.Add(ToPlural(NumberNameDict[groupNums[i]][0]));elsesections.Add(NumberNameDict[groupNums[i]][0]);remained-=whole*groupNums[i];if(remained!=0&&NeedInsertAnd(number,remained))//if(remained!=0&&remained<100)sections.Add(AndWords[0]);}if(remained!=0)sections.Add(toWords(remained));最终结果对于中文就是简单地将以上分散的字符串连接起来:StringBuildersb=newStringBuilder();for(inti=0;i<sections.Count-1;i++){sb.Append(sections[i]+Space);}sb.Append(sections.Last());returnsb.ToString();为实现个性化的字符输出,可以调用以下接口,对特定字符进行定义。///</summary>///<paramname="number">Thenumber</param>///<paramname="samples">///Thecharactersshallbeusedtoreplacethedefaultones.///<example>///Forexample,234002052bydefaultwillbeconvertedto"二亿三千四百万零二千零五十二",///butifthesamplesissetto"佰零壹贰叁肆拾",thentheoutputwillbe"贰亿叁千肆佰万零贰千零五拾贰"///anycharactersappearedinthesampleswillreplacethedefaultones,thus"贰"willreplaceany"二"sfordigitof"2".///</example>///</param>///<returns>Theconvertedstringinwords.</returns>privatestringtoWords(intnumber,stringsamples){stringresult=ToWords(number);foreach(charchinsamples){if(allCharacters.Contains(ch)&&WordNameDict.ContainsKey(ch.ToString())){intdigit=WordNameDict[ch.ToString()];if(digit>9&&!groupNums.Contains(digit))continue;stringdigitStr=NumberNameDict[digit][0];if(digitStr.Length!=1||digitStr[0]==ch)continue;result=result.Replace(digitStr[0],ch);}}returnresult;}中文转换为数字最大的挑战在于难以判断数字对应的位数,考虑到:高位应当出现在低位之前;如果高位出现在低位之后,意味着它们应通过相乘组合为一个更高位。低位出现在高位之后,意味着对之前的数字做个确认。因此我使用一个堆栈stack<int>实现数字的解析,相应代码如下。///<summary>///Functiontogetnumberfromsplitwords.///</summary>///<paramname="sectors">Wordsforeachdigitsofthenumber</param>///<returns>Thenumber</returns>protectedintfromWords(string[]sectors){intresult=0,current,lastGroup=1,temp,maxGroup=1;Stack<int>stack=newStack<int>();foreach(stringsinsectors){if(AllWords.Contains(s)){if(AndWords.Contains(s))continue;if(WordNameDict.ContainsKey(s)){current=WordNameDict[s];if(groupNums.Contains(current)){//Thecurrentgroupishigherthananyexistedgroup,thusthedigitsshallbeincreased:byMultiply!!!!if(current>=maxGroup){temp=stack.Pop();while(stack.Count!=0){temp+=stack.Pop();};temp*=current;stack.Push(temp);maxGroup*=current;lastGroup=1;}//Thecurrentgroupishigherthanthelastgroup,thusshallbeaddelseif(current>lastGroup){temp=0;while(stack.Peek()<current){temp+=stack.Pop();};temp*=current;stack.Push(temp);lastGroup=current;}else{temp=stack.Pop();temp*=current;stack.Push(temp);lastGroup=current;}}else{stack.Push(current);}}}elsethrownewException();}do{result+=stack.Pop();}while(stack.Count!=0);returnresult;}Toparsethestringtogetnumber,thetryParse()isrecommended.Collapse|CopyCode///<summary>///Themainfunctiontotrytoretrievenumberfromstringofwords.///</summary>///<paramname="numberInWords">Theoriginalwordstringofnumber</param>///<paramname="result">Theconvertednumberifsuccessful</param>///<returns>TRUEifparsesuccessfully.</returns>protectedvirtualbooltryParse(stringnumberInWords,outintresult){result=-1;try{stringwords=IsCaseSensitive?numberInWords.ToLower():numberInWords;string[]sectors=split(words);varcontained=fromsinsectorswhereAllWords.Contains(s)selects;result=fromWords(contained.ToArray());returntrue;}catch{returnfalse;}}最终对数字与中英文的转换结果如下:5:五==>520:廿==>2021:二十一==>2199:九十九==>99100:一百==>100102:一百零二==>102131:一百三十一==>131356:三百五十六==>356909:九百零九==>9091000:一千==>10001021:一千零二十一==>10212037:二千零三十七==>203712345:一万二千三百四十五==>1234531027:三万一千零二十七==>3102740002:四万零二==>4000290010:九万零一十==>90010100232300:一亿零二十三万二千三百==>100232300234002052:二亿三千四百万零二千零五十二==>2340020525:five==>520:twenty==>2021:twenty-one==>2199:ninety-nine==>99100:onehundred==>100102:onehundredandtwo==>102131:onehundredandthirty-one==>131356:threehundredsandfifty-six==>356100232300:onehundredmillionstwohundredsandthirty-twothousandsthreehundreds==>100232300234002052:twohundredsandthirty-fourmillionstwothousandsandfifty-two==>234002052572030013:五亿七千贰佰零叁万零壹拾叁==>572030013234002052:贰亿叁千肆佰万零贰千零五拾贰==>234002052100232300:OneHundredMillionsTwoHundredsAndThirtyTwoThousandsThreeHundreds==>100232300234002052:TwoHundredsAndThirtyFourMillionsTwoThousandsAndFiftyTwo==>234002052第壹佰零八张=108

解决方案

解决方案二:
楼主,不知小数字可否有考虑?

时间: 2024-07-29 00:36:17

通用数字中文互转换工具代码的相关文章

ASP.NET JSON字符串与实体类的互转换示例代码_实用技巧

还是先封装一个类吧! 这个类网上都可以找到的!有个这个类,一切都将变得简单了,哈哈. 复制代码 代码如下: using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Runtime.Serialization.Json;using System.ServiceModel.Web;///记得引用这个命名空间using System.IO;using System.Tex

php制作unicode解码工具代码分享

 php制作Unicode编码解码在线转换工具代码分享 代码如下: <?php function unicode_encode($name) {     $name = iconv('UTF-8', 'UCS-2', $name);     $len = strlen($name);     $str = '';     for ($i = 0; $i < $len - 1; $i = $i + 2)     {         $c = $name[$i];         $c2 = $n

ASP.NET JSON字符串与实体类的互转换的示例代码

 本篇文章主要是对ASP.NET JSON字符串与实体类的互转换的示例代码进行了介绍,需要的朋友可以过来参考下,希望对大家有所帮助 还是先封装一个类吧! 这个类网上都可以找到的!有个这个类,一切都将变得简单了,哈哈. 代码如下: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Runtime.Serialization.Json; using Sys

怎样构建中文文本标注工具?(附工具、代码、论文等资源)

项目地址: https://github.com/crownpku/Chinese-Annotator 自然语言处理的大部分任务是监督学习问题.序列标注问题如中文分词.命名实体识别,分类问题如关系识别.情感分析.意图分析等,均需要标注数据进行模型训练.在深度学习大行其道的今天,基于深度学习的 NLP 模型更是数据饥渴.  最前沿的 NLP 技术往往首先针对英文语料.英文 NLP 的生态很好,针对不同有意思的问题都有不少大规模语料公开供大家研究,如斯坦福的 SQuAD 阅读理解语料.中文方面开源语

asp.net中文简体转换成繁体代码

这是一款简单的asp教程.net教程中文简体转换成繁体代码哦,这款中文简单转换繁体的简单代码,挨过来繁体转换成简体也一样. //Form1.Designer.cs namespace TestEnCode { partial class Form1 { /// <summary> /// 必需的设计器变量. /// </summary> private System.ComponentModel.IContainer components = null; /// <summa

php将中文转换拼音代码

php教程将中文转换拼音代码 本程序的实现原是是把汉字转换成相对应机内码,然后再定义好相对就的拼音,这样中文转换拼音就只是查询问题了. $d=array( array("a",-20319), array("ai",-20317), array("an",-20304), array("ang",-20295), array("ao",-20292), array("ba",-20283

10 个免费的 HTML 视频转换工具 【已翻译100%】

现在,人们都喜欢看视频目前,大多数的人都是看视频在线或移动.因此,视频必须有正确的格式,可以通过手机或支持系统.因此,人们需要一些应用程序的工具,将有助于他们现有的文件格式转换成当前的格式如MP4,WebM,和OGG.另外,如果你想改变音频文件格式,然后你也不用担心,因为有些工具转换成所需格式的音频格式. 一些工具都是自动化的,这意味着它将现有的文件格式转换成工作文件格式.如果一个人不知道的视频转换工具,那就不需要担心,因为这里有一些特定的工具,通过它你可以将文件转换为有效和高效的方式,他们用你

Unicode编码解码在线转换工具

Unicode编码解码在线转换工具 Unicode 是基于通用字符集(Universal Character Set)的标准来发展,并且同时也以书本的形式(The Unicode Standard,目前第五版由Addison-Wesley Professional出版,ISBN-10: 0321480910)对外发表. 2006年7月的最新版本的 Unicode 是5.0版本. 2005年3月31日推出的Unicode 4.1.0 .另外,5.0 Beta于2005年12月12日推出,5.2版本

使用C#实现阿拉伯数字到大写中文的转换_C#教程

 先记下来,以备后用! /// <summary> /// 金额转为大写金额 /// </summary> public class MoneyConvertChinese { /// <summary> /// 金额转为大写金额 /// </summary> /// <param name="LowerMoney"></param> /// <returns></returns> publ