一个工具类,提供了阿拉伯数字和中文金额之间的相互转化。直接可以使用
代码如下:
- package org.pengxiaofei.common.tools;
- import java.text.NumberFormat;
- import java.util.HashMap;
- import java.util.Map;
- /***
- *本工具类主要提供数字和中文金额的相互转化
- *
- * @author 朱鹏飞
- *
- *@version 1.0
- */
- public class NumberConvertUtil {
- /**
- * 将数字转成中文金额
- *
- * @param num
- * @return
- */
- public static String translate(int num) {
- return translate((long) num);
- }
- /**
- * 将数字转成以逗号分开
- *
- * @param num
- * @return
- */
- public static String getCommaNumber(long num) {
- return NumberFormat.getInstance().format(num) + ".00";
- }
- /**
- * 将数字转成中文金额
- *
- * @param num
- * @return
- */
- public static String translate(long num) {
- return translate(num, true);
- }
- /**
- * 将数字转成中文
- *
- * @param num
- * @param isHead
- * 是否为高位
- * @return
- */
- private static String translate(long num, boolean isHead) {
- StringBuilder result = new StringBuilder();
- // 求出数字的总位数
- int size = String.valueOf(num).length();
- // 数字可分成万的端数
- int powerNum = (size % 4 == 0) ? (size / 4 - 1) : (size / 4);
- // 先取出最高一段,转换成中文数字
- result.append(getBelowTenThousandString(
- (int) (num / getPower(powerNum)), isHead));
- // 添加最高端的万级别的单位
- result.append(getBigUnit(powerNum));
- // 再递归添加低端位数字转化成的中文
- long leftNum = num % (long) getPower(powerNum);
- if (leftNum != 0) {
- result.append(translate(leftNum, false));
- }
- return result.toString();
- }
- /**
- * 获取万以下的数字的表示法
- *
- * @param num
- * 数字
- * @param isHead
- * 是否为一个数字的高位,如1234不是10001234的高位,而1000是10001234的高位
- * @return
- */
- private static String getBelowTenThousandString(int num, boolean isHead) {
- char[] chineseNum = { '零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖' };
- char[] smallUnit = { '仟', '佰', '拾' };
- // 先判断是否为零
- if (num == 0) {
- // 是高位则添加零(因为它就是零),否则返回空字符串
- if (isHead) {
- return String.valueOf(chineseNum[0]);
- } else {
- return "";
- }
- }
- // 仟、佰、拾、个位是否为零
- boolean[] isNotZero = new boolean[4];
- StringBuilder result = new StringBuilder();
- // 低位如果小于1000就要就加零连接高位
- if (num < 1000 && !isHead) {
- result.append(chineseNum[0]);
- }
- // 千位的处理
- int leftNum = num;
- if (num >= 1000) {
- result.append(chineseNum[num / 1000]);
- result.append(smallUnit[0]);
- isNotZero[0] = true;
- }
- // 百位的处理
- leftNum = num % 1000;
- if (leftNum / 100 != 0) {
- result.append(chineseNum[leftNum / 100]);
- result.append(smallUnit[1]);
- isNotZero[1] = true;
- } else {
- if (isNotZero[0] && ((num % 1000) != 0)) {
- result.append(chineseNum[0]);
- }
- }
- // 十位的处理
- leftNum = num % 100;
- if (leftNum / 10 != 0) {
- if (leftNum / 10 != 1 || num >= 100) {
- result.append(chineseNum[leftNum / 10]);
- }
- result.append(smallUnit[2]);
- isNotZero[2] = true;
- } else {
- if (isNotZero[1] && ((num % 100) != 0)) {
- result.append(chineseNum[0]);
- }
- }
- // 个位的处理
- leftNum = num % 10;
- if (leftNum != 0) {
- result.append(chineseNum[leftNum]);
- isNotZero[3] = true;
- }
- return result.toString();
- }
- /**
- * 获取万的num次方的单位(万,亿,万亿,亿亿之类的)
- *
- * @param num
- * @return
- */
- private static String getBigUnit(int num) {
- char[] bigUnit = { '万', '亿' };
- if (num == 0) {
- return "";
- }
- String unitString = getBigUnit(num - 1);
- return (unitString.indexOf(bigUnit[0]) > -1) ?
- (unitString.replace(bigUnit[0], bigUnit[1]))
- : (bigUnit[0] + unitString);
- }
- /**
- * 获取一万的size次幂
- *
- * @param size
- * @return
- */
- private static Long getPower(int size) {
- if (size > 4) {
- throw new IllegalArgumentException("数字超过范围");
- }
- Long result = 1L;
- for (int i = 0; i < size; i++) {
- result *= 10000L;
- }
- return result;
- }
- /***
- *中文金额字符
- */
- private static final Character[] CN_NUMERIC = { '一', '二', '三', '四', '五',
- '六', '七', '八', '九', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖',
- '十', '百', '千', '拾', '佰', '仟', '万', '亿', '○', 'O', '零' };
- private static Map<Character, Integer> cnNumeric = null;
- static {
- cnNumeric = new HashMap<Character, Integer>(40, 0.85f);
- for (int j = 0; j < 9; j++)
- cnNumeric.put(CN_NUMERIC[j], j + 1);
- for (int j = 9; j < 18; j++)
- cnNumeric.put(CN_NUMERIC[j], 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);
- }
- public static int isCNNumeric(char c) {
- Integer i = cnNumeric.get(c);
- if (i == null)
- return -1;
- return i.intValue();
- }
- /***
- * 将中文金额数字转换为阿拉伯数字
- * @param cnn
- * @param flag 是否有繁体的中文单位(佰,仟,拾)
- * @return
- */
- 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('零', ' ');
- // 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());
- else
- cnn = "";
- if (cnn.length() == 1) {
- int arbic = isCNNumeric(cnn.charAt(0));
- if (arbic <= 10)
- val += arbic * 10000000;
- cnn = "";
- }
- }
- wan = cnn.lastIndexOf('万');
- if (wan > -1) {
- val += cnNumericToArabic(cnn.substring(0, wan), false) * 10000;
- if (wan < cnn.length() - 1)
- cnn = cnn.substring(wan + 1, cnn.length());
- else
- cnn = "";
- if (cnn.length() == 1) {
- int arbic = isCNNumeric(cnn.charAt(0));
- if (arbic <= 10)
- val += arbic * 1000;
- cnn = "";
- }
- }
- qian = cnn.lastIndexOf('千');
- if (qian > -1) {
- val += cnNumericToArabic(cnn.substring(0, qian), false) * 1000;
- if (qian < cnn.length() - 1)
- cnn = cnn.substring(qian + 1, cnn.length());
- else
- cnn = "";
- if (cnn.length() == 1) {
- int arbic = isCNNumeric(cnn.charAt(0));
- if (arbic <= 10)
- val += arbic * 100;
- cnn = "";
- }
- }
- bai = cnn.lastIndexOf('百');
- if (bai > -1) {
- val += cnNumericToArabic(cnn.substring(0, bai), false) * 100;
- if (bai < cnn.length() - 1)
- cnn = cnn.substring(bai + 1, cnn.length());
- else
- cnn = "";
- if (cnn.length() == 1) {
- int arbic = isCNNumeric(cnn.charAt(0));
- if (arbic <= 10)
- val += arbic * 10;
- cnn = "";
- }
- }
- shi = cnn.lastIndexOf('十');
- if (shi > -1) {
- if (shi == 0)
- val += 1 * 10;
- else
- val += cnNumericToArabic(cnn.substring(0, shi), false) * 10;
- if (shi < cnn.length() - 1)
- cnn = cnn.substring(shi + 1, cnn.length());
- else
- cnn = "";
- }
- 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;
- }
- }