1,数字转大写

  1. package org.xxx.util;
  2. import java.util.ArrayList;
  3. import java.util.Collections;
  4. import java.util.regex.Pattern;
  5. public class NumToCase {
  6. /**
  7. * 单位进位,中文默认为4位即(万、亿)
  8. */
  9. public static int UNIT_STEP = 4;
  10. /**
  11. * 单位
  12. */
  13. public static String[] CN_UNITS = new String[] { "个", "十", "百", "千", "万", "十",
  14. "百", "千", "亿", "十", "百", "千", "万" };
  15. /**
  16. * 汉字
  17. */
  18. public static String[] CN_CHARS = new String[] { "零", "一", "二", "三", "四",
  19. "五", "六", "七", "八", "九" };
  20. /**
  21. * 将阿拉伯数字转换为中文数字123=》一二三
  22. *
  23. * @param srcNum
  24. * @return
  25. */
  26. public static String getCNNum(int srcNum) {
  27. String desCNNum = "";
  28. if (srcNum <= 0)
  29. desCNNum = "零";
  30. else {
  31. int singleDigit;
  32. while (srcNum > 0) {
  33. singleDigit = srcNum % 10;
  34. desCNNum = String.valueOf(CN_CHARS[singleDigit]) + desCNNum;
  35. srcNum /= 10;
  36. }
  37. }
  38. return desCNNum;
  39. }
  40. /**
  41. * 数值转换为中文字符串 如2转化为贰
  42. */
  43. public String cvt(long num) {
  44. return this.cvt(num, false);
  45. }
  46. /**
  47. * 数值转换为中文字符串(口语化)
  48. *
  49. * @param num
  50. * 需要转换的数值
  51. * @param isColloquial
  52. * 是否口语化。例如12转换为'十二'而不是'一十二'。
  53. * @return
  54. */
  55. public String cvt(String num, boolean isColloquial) {
  56. int integer, decimal;
  57. StringBuffer strs = new StringBuffer(32);
  58. String[] splitNum = num.split("\\.");
  59. integer = Integer.parseInt(splitNum[0]); //整数部分
  60. decimal = Integer.parseInt(splitNum[1]); //小数部分
  61. String[] result_1 =convert(integer, isColloquial);
  62. for (String str1 : result_1)
  63. strs.append(str1);
  64. if(decimal==0){//小数部分为0时
  65. return strs.toString();
  66. }else{
  67. String result_2 =getCNNum(decimal); //例如5.32,小数部分展示三二,而不是三十二
  68. strs.append("点");
  69. strs.append(result_2);
  70. return strs.toString();
  71. }
  72. }
  73. /*
  74. * 对于int,long类型的数据处理
  75. */
  76. public String cvt(long num, boolean isColloquial) {
  77. String[] result = this.convert(num, isColloquial);
  78. StringBuffer strs = new StringBuffer(32);
  79. for (String str : result) {
  80. strs.append(str);
  81. }
  82. return strs.toString();
  83. }
  84. /**
  85. * 将数值转换为中文
  86. *
  87. * @param num
  88. * 需要转换的数值
  89. * @param isColloquial
  90. * 是否口语化。例如12转换为'十二'而不是'一十二'。
  91. * @return
  92. */
  93. public String[] convert(long num, boolean isColloquial) {
  94. if (num < 10) {// 10以下直接返回对应汉字
  95. return new String[] { CN_CHARS[(int) num] };// ASCII2int
  96. }
  97. char[] chars = String.valueOf(num).toCharArray();
  98. if (chars.length > CN_UNITS.length) {// 超过单位表示范围的返回空
  99. return new String[] {};
  100. }
  101. boolean isLastUnitStep = false;// 记录上次单位进位
  102. ArrayList<String> cnchars = new ArrayList<String>(chars.length * 2);// 创建数组,将数字填入单位对应的位置
  103. for (int pos = chars.length - 1; pos >= 0; pos--) {// 从低位向高位循环
  104. char ch = chars[pos];
  105. String cnChar = CN_CHARS[ch - '0'];// ascii2int 汉字
  106. int unitPos = chars.length - pos - 1;// 对应的单位坐标
  107. String cnUnit = CN_UNITS[unitPos];// 单位
  108. boolean isZero = (ch == '0');// 是否为0
  109. boolean isZeroLow = (pos + 1 < chars.length && chars[pos + 1] == '0');// 是否低位为0
  110. boolean isUnitStep = (unitPos >= UNIT_STEP && (unitPos % UNIT_STEP == 0));// 当前位是否需要单位进位
  111. if (isUnitStep && isLastUnitStep) {// 去除相邻的上一个单位进位
  112. int size = cnchars.size();
  113. cnchars.remove(size - 1);
  114. if (!CN_CHARS[0].equals(cnchars.get(size - 2))) {// 补0
  115. cnchars.add(CN_CHARS[0]);
  116. }
  117. }
  118. if (isUnitStep || !isZero) {// 单位进位(万、亿),或者非0时加上单位
  119. cnchars.add(cnUnit);
  120. isLastUnitStep = isUnitStep;
  121. }
  122. if (isZero && (isZeroLow || isUnitStep)) {// 当前位为0低位为0,或者当前位为0并且为单位进位时进行省略
  123. continue;
  124. }
  125. cnchars.add(cnChar);
  126. isLastUnitStep = false;
  127. }
  128. Collections.reverse(cnchars);
  129. // 清除最后一位的0
  130. int chSize = cnchars.size();
  131. String chEnd = cnchars.get(chSize - 1);
  132. if (CN_CHARS[0].equals(chEnd) || CN_UNITS[0].equals(chEnd)) {
  133. cnchars.remove(chSize - 1);
  134. }
  135. // 口语化处理
  136. if (isColloquial) {
  137. String chFirst = cnchars.get(0);
  138. String chSecond = cnchars.get(1);
  139. if (chFirst.equals(CN_CHARS[1]) && chSecond.startsWith(CN_UNITS[1])) {// 是否以'一'开头,紧跟'十'
  140. cnchars.remove(0);
  141. }
  142. }
  143. return cnchars.toArray(new String[] {});
  144. }
  145. public static void main(String[] args) {
  146. NumToCase temp = new NumToCase();
  147. String i ="40.3";
  148. int t = 2234;
  149. double j = 221.23;
  150. System.out.println(temp.cvt(i,true));//四十点三
  151. System.out.println(temp.cvt(t,true));//二千二百三十四
  152. System.out.println(temp.cvt(""+j,true));//二百二十一点二三
  153. }
  154. }

2,数字转汉字

  1. package org.xxx.util;
  2. import java.util.StringTokenizer;
  3. public class StringHelper {
  4. public static String[] strToStrArray(String str, String separator) {
  5. return strToStrArrayManager(str, separator);
  6. }
  7. private static String[] strToStrArrayManager(String str, String separator) {
  8. StringTokenizer strTokens = new StringTokenizer(str, separator);
  9. String[] strArray = new String[strTokens.countTokens()];
  10. int i = 0;
  11. while (strTokens.hasMoreTokens()) {
  12. strArray[i] = strTokens.nextToken().trim();
  13. i++;
  14. }
  15. return strArray;
  16. }
  17. }
  1. package org.xxx.util;
  2. import java.text.DecimalFormat;
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. public class NumToChinese {
  6. private static final String[] BEFORE_SCALE = { "万", "仟", "佰", "拾", "亿", "仟", "佰", "拾", "万", "仟", "佰", "拾", "" };
  7. // private static final String[] AFTER_SCALE = { "角", "分" };
  8. private static final String[] AFTER_SCALE = { "", "" };
  9. private static final String DEFAULT_PATH_SEPARATOR = ".";
  10. private static final Map<String, String> NUMBER_MAPPING = new HashMap<String, String>();
  11. static {
  12. NUMBER_MAPPING.put("0", "零");
  13. NUMBER_MAPPING.put("1", "壹");
  14. NUMBER_MAPPING.put("2", "贰");
  15. NUMBER_MAPPING.put("3", "叁");
  16. NUMBER_MAPPING.put("4", "肆");
  17. NUMBER_MAPPING.put("5", "伍");
  18. NUMBER_MAPPING.put("6", "陆");
  19. NUMBER_MAPPING.put("7", "柒");
  20. NUMBER_MAPPING.put("8", "捌");
  21. NUMBER_MAPPING.put("9", "玖");
  22. }
  23. public static String getChineseNumber(String number) {
  24. return getChineseNumber(number, null, null);
  25. }
  26. public static String getChineseNumber(String number, String unit, String postfix) {
  27. String[] numbers = StringHelper.strToStrArray(number, DEFAULT_PATH_SEPARATOR);
  28. if (numbers.length > 2) {
  29. new NumberFormatException("数字格式错误!");
  30. }
  31. int length = numbers[0].length();
  32. int isZero = 0;
  33. StringBuffer result = new StringBuffer();
  34. for (int i = 0; i < length; i++) {
  35. String digit = String.valueOf(numbers[0].charAt(i));
  36. boolean allZero = true; // 如果后继的全部是零,则跳出
  37. for (int j = i; j < length; j++) {
  38. if (numbers[0].charAt(j) != '0') {
  39. allZero = false;
  40. break;
  41. }
  42. }
  43. if (allZero) {
  44. boolean hasValue = false;
  45. for (int z = i; z >= 0; z--) {
  46. if (numbers[0].charAt(z) != '0' && length - z <= 7 && length - z >= 5) {
  47. hasValue = true;
  48. break;
  49. }
  50. }
  51. // 加万单位
  52. if ( ( length - i > 4 && length <= 8 ) || ( hasValue && length - i > 4 )) {
  53. result.append(BEFORE_SCALE[8]);
  54. }
  55. // 加亿单位
  56. if (length - i >= 9) {
  57. result.append(BEFORE_SCALE[4]);
  58. }
  59. break;
  60. }
  61. if (length < 9 && length - i == 5) {
  62. if (!"0".equals(digit) && isZero > 0) {
  63. result.append(NUMBER_MAPPING.get("0"));
  64. }
  65. if ("0".equals(digit)) {
  66. result.append(BEFORE_SCALE[8]);
  67. if (isZero > 0) {
  68. result.append(NUMBER_MAPPING.get("0"));
  69. }
  70. continue;
  71. }
  72. }
  73. if ("0".equals(digit) && length > 9 && length - i == 9) {
  74. result.append(BEFORE_SCALE[4]);
  75. continue;
  76. }
  77. if (isZero < 1 || !"0".equals(digit)) {
  78. if ("0".equals(digit)) {
  79. if (length - i != 6 && length - i != 7) {
  80. result.append(NUMBER_MAPPING.get(digit));
  81. }
  82. } else {
  83. result.append(NUMBER_MAPPING.get(digit));
  84. }
  85. if (!"0".equals(digit)) {
  86. result.append(BEFORE_SCALE[BEFORE_SCALE.length - length + i]);
  87. }
  88. }
  89. if ("0".equals(digit)) {
  90. isZero++;
  91. } else {
  92. isZero = 0;
  93. }
  94. }
  95. // result.append(unit == null ? "圆" : result.append(unit));
  96. result.append(unit == null ? "" : result.append(unit));
  97. if (numbers.length == 1) {
  98. // result.append(postfix == null ? "整" : result.append(postfix));
  99. result.append(postfix == null ? "" : result.append(postfix));
  100. return result.toString();
  101. }
  102. length = numbers[1].length();
  103. for (int j = 0; j < length; j++) {
  104. if (j > 2) {
  105. break;
  106. }
  107. if (numbers[1].charAt(j) == '0') {
  108. continue;
  109. }
  110. result.append(NUMBER_MAPPING.get(String.valueOf(numbers[1].charAt(j))));
  111. result.append(AFTER_SCALE[j]);
  112. }
  113. // result.append(postfix == null ? "整" : result.append(postfix));
  114. result.append(postfix == null ? "" : result.append(postfix));
  115. return result.toString();
  116. }
  117. public static String getChineseNumber(int number) {
  118. return getChineseNumber(new Integer(number));
  119. }
  120. public static String getChineseNumber(int number, String unit, String postfix) {
  121. return getChineseNumber(new Integer(number), unit, postfix);
  122. }
  123. public static String getChineseNumber(Long number) {
  124. return getChineseNumber(number.toString(), null, null);
  125. }
  126. public static String getChineseNumber(Integer number) {
  127. return getChineseNumber(number.toString(), null, null);
  128. }
  129. public static String getChineseNumber(Integer number, String unit, String postfix) {
  130. return getChineseNumber(number.toString(), unit, postfix);
  131. }
  132. public static String getChineseNumber(Long number, String unit, String postfix) {
  133. return getChineseNumber(number.toString(), unit, postfix);
  134. }
  135. public static String getChineseNumber(long number) {
  136. return getChineseNumber(new Long(number));
  137. }
  138. public static String getChineseNumber(long number, String unit, String postfix) {
  139. return getChineseNumber(new Long(number), unit, postfix);
  140. }
  141. public static String getChineseNumber(double number, String unit, String postfix) {
  142. DecimalFormat f = (DecimalFormat) DecimalFormat.getInstance();
  143. f.applyLocalizedPattern("#.##");
  144. return getChineseNumber(f.format(number), unit, postfix);
  145. }
  146. public static String getChineseNumber(double number) {
  147. return getChineseNumber(number, null, null);
  148. }
  149. public static String getChineseNumber(Double number) {
  150. return getChineseNumber(number.doubleValue());
  151. }
  152. public static String getChineseNumber(Double number, String unit, String postfix) {
  153. return getChineseNumber(number.doubleValue(), unit, postfix);
  154. }
  155. public static void main(String[] args) {
  156. System.out.println(getChineseNumber(2019));
  157. System.out.println(getChineseNumber(2345.632234));
  158. System.out.println(getChineseNumber(2345.68657));
  159. System.out.println(getChineseNumber(235667));
  160. }
  161. }