资源预览内容
第1页 / 共5页
第2页 / 共5页
第3页 / 共5页
第4页 / 共5页
第5页 / 共5页
亲,该文档总共5页全部预览完了,如果喜欢就下载吧!
资源描述
java中文数字转换成阿拉伯数字import java.util.HashMap;import java.util.Map;/* a class to deal with Chinese numeric. * author chenxin link http:/www.webssky.com*/public class CNNMFilter /* chinese numeric chars. * i have put the chars into the lexicon file lex-cn-numeric.lex for the old* version. its better to follow the current work.*/private static final Character CN_NUMERIC = 一, 二, 三, 四, 五,六, 七, 八, 九, 壹, 贰, 叁, 肆, 伍, 陆, 柒, 捌, 玖,十, 百, 千, 拾, 佰, 仟, 万, 亿, , , 零 ;private static Map cnNumeric = null;static cnNumeric = new HashMap(40, 0.85f);for (int j = 0; j 9; j+)cnNumeric.put(CN_NUMERICj, j + 1);for (int j = 9; j 18; j+)cnNumeric.put(CN_NUMERICj, j - 8);cnNumeric.put(两, 2);cnNumeric.put(十, 10);cnNumeric.put(拾, 10);cnNumeric.put(百, 100);cnNumeric.put(佰, 100);cnNumeric.put(千, 1000);cnNumeric.put(仟, 1000);cnNumeric.put(万, 10000);cnNumeric.put(亿, 100000000);/* check the given char is chinese numeric or not. * param c* * return boolean true yes and false for not.*/public static int isCNNumeric(char c) Integer i = cnNumeric.get(c);if (i = null)return -1;return i.intValue();/* a static method to turn the Chinese numeric to Arabic numbers.* param cnn* param boolea* flag* return int*/public static int cnNumericToArabic(String cnn, boolean flag) cnn = cnn.trim();if (cnn.length() = 1)return isCNNumeric(cnn.charAt(0);if (flag)cnn = cnn.replace(佰, 百).replace(仟, 千).replace(拾, 十).replace(零, ).replace(元, );/ System.out.println(cnn);int yi = -1, wan = -1, qian = -1, bai = -1, shi = -1;int val = 0;yi = cnn.lastIndexOf(亿);if (yi -1) val += cnNumericToArabic(cnn.substring(0, yi), false) * 100000000;if (yi cnn.length() - 1)cnn = cnn.substring(yi + 1, cnn.length();elsecnn = ;if (cnn.length() = 1) int arbic = isCNNumeric(cnn.charAt(0);if (arbic -1) val += cnNumericToArabic(cnn.substring(0, wan), false) * 10000;if (wan cnn.length() - 1)cnn = cnn.substring(wan + 1, cnn.length();elsecnn = ;if (cnn.length() = 1) int arbic = isCNNumeric(cnn.charAt(0);if (arbic -1) val += cnNumericToArabic(cnn.substring(0, qian), false) * 1000;if (qian cnn.length() - 1)cnn = cnn.substring(qian + 1, cnn.length();elsecnn = ;if (cnn.length() = 1) int arbic = isCNNumeric(cnn.charAt(0);if (arbic -1) val += cnNumericToArabic(cnn.substring(0, bai), false) * 100;if (bai cnn.length() - 1)cnn = cnn.substring(bai + 1, cnn.length();elsecnn = ;if (cnn.length() = 1) int arbic = isCNNumeric(cnn.charAt(0);if (arbic -1) if (shi = 0)val += 1 * 10;elseval += cnNumericToArabic(cnn.substring(0, shi), false) * 10;if (shi cnn.length() - 1)cnn = cnn.substring(shi + 1, cnn.length();elsecnn = ;cnn = cnn.trim();for (int j = 0; j cnn.length(); j+)val += isCNNumeric(cnn.charAt(j)* Math.pow(10, cnn.length() - j - 1);return val;public static int qCNNumericToArabic(String cnn) int val = 0;cnn = cnn.trim();for (int j = 0; j cnn.length(); j+)val += isCNNumeric(cnn.charAt(j)* Math.pow(10, cnn.length() - j - 1);return val;/* public static void main(String args) ADictionary.isCNNumeric(一);* int val = 0; long s = System.nanoTime(); /val =* cnNumericToArabic(三亿二千零六万七千五百六, true); /val =* cnNumericToArabic(一九九八, true); long e = System.nanoTime();* System.out.format(Done+val+, cost: %.5fsecn, (float)(e - s) /* 1E9); */ 该代码片段来自于: http:/www.sharejs.com/codes/java/5577
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号