汉字数字转阿拉伯数字 和 阿拉伯数字+千/百/万/亿 转阿拉伯数字

灰太狼 2022-05-31 12:26 573阅读 0赞

感谢这位辛苦的大佬: http://blog.csdn.net/jjfly999/article/details/51052492\#insertcode

项目的需要,把中文的汉字转成纯阿拉伯数字

比如 三千—>3000

但是有的时候可能会输入3千,这样处理就比较麻烦一点

在网上 找了一篇文章(上面的链接),当有大数字的时候,效果不是特别好,所以进行了改进

改进后还可以识别一些变态的写法,比如:100千,300十,30千万,等等

上代码,一共3个java文件:

  1. package com.test;
  2. import java.math.BigInteger;
  3. public class ChineseChangeToNumber {
  4. public BigInteger ChineseToNumber(String str){
  5. String str1 = new String();
  6. String str2 = new String();
  7. String str3 = new String();
  8. int k = 0;
  9. boolean dealflag = true;
  10. for(int i=0;i<str.length();i++){//先把字符串中的“零”除去
  11. if('零' == (str.charAt(i))){
  12. str = str.substring(0, i) + str.substring(i+1);
  13. }
  14. }
  15. String chineseNum = str;
  16. for(int i=0;i<chineseNum.length();i++){
  17. if(chineseNum.charAt(i) == '亿'){
  18. str1 = chineseNum.substring(0,i);//截取亿前面的数字,逐个对照表格,然后转换
  19. k = i+1;
  20. dealflag = false;//已经处理
  21. if(!chineseNum.contains("万") && chineseNum.length()>i) {
  22. str3 = str.substring(i+1,chineseNum.length());
  23. }
  24. }
  25. if(chineseNum.charAt(i) == '万'){
  26. str2 = chineseNum.substring(k,i);
  27. str3 = str.substring(i+1);
  28. dealflag = false;//已经处理
  29. }
  30. }
  31. if(dealflag){//如果没有处理
  32. str3 = chineseNum;
  33. }
  34. BigInteger str1Int=sectionChinese(str1).multiply(new BigInteger("100000000"));
  35. BigInteger str2Int=sectionChinese(str2). multiply(new BigInteger("10000"));
  36. BigInteger str3Int=sectionChinese(str3);
  37. BigInteger result =str1Int.add(str2Int).add(str3Int);
  38. return result;
  39. }
  40. public BigInteger sectionChinese(String str){
  41. int value = 0;
  42. int sectionNum = 0;
  43. for(int i=0;i<str.length();i++){
  44. int v = (int) Tool.intList.get(str.charAt(i));
  45. if(v>=0 && v<=9) {
  46. if(sectionNum==0) {
  47. sectionNum=v;
  48. }else {
  49. sectionNum =sectionNum*10+ v;
  50. }
  51. if(i == str.length()-1) {
  52. value=sectionNum+value;
  53. }
  54. }
  55. if(v == 10 || v == 100 || v == 1000) {
  56. if(i==0) {
  57. sectionNum=1;
  58. }
  59. sectionNum=sectionNum*v;
  60. value=sectionNum+value;
  61. sectionNum=0;
  62. }
  63. }
  64. return new BigInteger(String.valueOf(value));
  65. }
  66. }
  67. package com.test;
  68. import java.util.HashMap;
  69. import java.util.regex.Matcher;
  70. import java.util.regex.Pattern;
  71. public class Tool {
  72. //数字位
  73. public static String[] chnNumChar = {"零","一","二","三","四","五","六","七","八","九"};
  74. public static char[] chnNumChinese = {'零','一','二','三','四','五','六','七','八','九'};
  75. //节权位
  76. public static String[] chnUnitSection = {"","万","亿","万亿"};
  77. //权位
  78. public static String[] chnUnitChar = {"","十","百","千"};
  79. public static HashMap intList = new HashMap();
  80. static{
  81. for(int i=0;i<chnNumChar.length;i++){
  82. intList.put(chnNumChinese[i], i);
  83. }
  84. for(char i=0;i<100;i++){
  85. intList.put(new Character((char)(48+i)), new Integer(i));
  86. }
  87. intList.put('十',10);
  88. intList.put('百',100);
  89. intList.put('千', 1000);
  90. }
  91. }

下面的是测试类 :

  1. package com.test;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Main ma = new Main();
  5. Tool to = new Tool();
  6. ma.initMain();
  7. }
  8. public void initMain(){
  9. testChineseToNumber();
  10. }
  11. public void testChineseToNumber(){
  12. ChineseChangeToNumber chineseToNumber = new ChineseChangeToNumber();
  13. String a[]= { "1","22","333","444",
  14. "三","六","八",
  15. "二十","八十",
  16. "一百","三百",
  17. "一千",
  18. "一万","十万","三十万","二十万","三百万",
  19. "一亿","十亿","二十亿","四百亿","四千亿",
  20. "二十三","四十五","三十六",
  21. "三百二十","六百三十","八百九十",
  22. "三千六百二十","五千七百三十",
  23. "一万二千二百三十六","十万八千六百三十四","二十万三千","二十万零三千","一百万零八千零六十","七百万零五十六","四百万","一千万零八百六十四","八千万零九百八十二",
  24. "一亿零八十","一亿八千六百三十二万三千六百五十二","十亿六千零五十四万三千","九亿零八百六十二", //1,
  25. "三百亿","三百亿零四百八十万三千六百二十","三百亿零三十","三百亿三千万","三百亿零三万",
  26. "3千","4万","4十",
  27. "1千万","3千万","50千万",
  28. "3000万","3456万","34561万",
  29. "3000万800","3亿800万",
  30. "3亿80万88","200亿40万123",
  31. "30千","300十","40十万","40百万",
  32. "30千20","50千4","300十5","40百万80",
  33. };
  34. for(int i=0;i<a.length;i++) {
  35. System.out.println(a[i]+"--->"+chineseToNumber.ChineseToNumber(a[i]));
  36. }
  37. }
  38. }

改进后的 源码 ,来看一下 测试的结果:

  1. 1--->1
  2. 22--->22
  3. 333--->333
  4. 444--->444
  5. 三--->3
  6. 六--->6
  7. 八--->8
  8. 二十--->20
  9. 八十--->80
  10. 一百--->100
  11. 三百--->300
  12. 一千--->1000
  13. 一万--->10000
  14. 十万--->100000
  15. 三十万--->300000
  16. 二十万--->200000
  17. 三百万--->3000000
  18. 一亿--->100000000
  19. 十亿--->1000000000
  20. 二十亿--->2000000000
  21. 四百亿--->40000000000
  22. 四千亿--->400000000000
  23. 二十三--->23
  24. 四十五--->45
  25. 三十六--->36
  26. 三百二十--->320
  27. 六百三十--->630
  28. 八百九十--->890
  29. 三千六百二十--->3620
  30. 五千七百三十--->5730
  31. 一万二千二百三十六--->12236
  32. 十万八千六百三十四--->108634
  33. 二十万三千--->203000
  34. 二十万零三千--->203000
  35. 一百万零八千零六十--->1008060
  36. 七百万零五十六--->7000056
  37. 四百万--->4000000
  38. 一千万零八百六十四--->10000864
  39. 八千万零九百八十二--->80000982
  40. 一亿零八十--->100000080
  41. 一亿八千六百三十二万三千六百五十二--->186323652
  42. 十亿六千零五十四万三千--->1060543000
  43. 九亿零八百六十二--->900000862
  44. 三百亿--->30000000000
  45. 三百亿零四百八十万三千六百二十--->30004803620
  46. 三百亿零三十--->30000000030
  47. 三百亿三千万--->30030000000
  48. 三百亿零三万--->30000030000
  49. 3千--->3000
  50. 4万--->40000
  51. 4十--->40
  52. 1千万--->10000000
  53. 3千万--->30000000
  54. 50千万--->500000000
  55. 3000万--->30000000
  56. 3456万--->34560000
  57. 34561万--->345610000
  58. 3000800--->30000800
  59. 3亿800万--->308000000
  60. 3亿8088--->300800088
  61. 200亿40123--->20000400123
  62. 30千--->30000
  63. 300十--->3000
  64. 40十万--->4000000
  65. 40百万--->40000000
  66. 3020--->30020
  67. 504--->50004
  68. 3005--->3005
  69. 40百万80--->40000080

转换的效果还是可以的.

发表评论

表情:
评论列表 (有 0 条评论,573人围观)

还没有评论,来说两句吧...

相关阅读