java中文数字与阿拉伯数字相互转换

最近有一个业务需要用到中文数字与阿拉伯数字相互转换,废话不多说直接上代码.

中文数字/阿拉伯数字互转第一次完善:https://blog.csdn.net/qq_26896085/article/details/100081049

package cn.utstarcom.idpvoice.util;

/**
 * 工具类,用于将汉语的数字转换为阿拉伯数字
 * @author utsc1243
 * @date 2019年6月12日
 */
public class ChineseNumToArabicNumUtil {
	static char[] cnArr = new char [] {'一','二','三','四','五','六','七','八','九'};
	static char[] chArr = new char [] {'十','百','千','万','亿'};
	static String allChineseNum = "零一二三四五六七八九十百千万亿";
	
	/**
	 * 将汉字中的数字转换为阿拉伯数字
	 * @param chineseNum
	 * @return
	 */
	public static int chineseNumToArabicNum(String chineseNum) {
        int result = 0;
        int temp = 1;//存放一个单位的数字如:十万
        int count = 0;//判断是否有chArr
        for (int i = 0; i < chineseNum.length(); i++) {
            boolean b = true;//判断是否是chArr
            char c = chineseNum.charAt(i);
            for (int j = 0; j < cnArr.length; j++) {//非单位,即数字
                if (c == cnArr[j]) {
                    if(0 != count){//添加下一个单位之前,先把上一个单位值添加到结果中
                        result += temp;
                        temp = 1;
                        count = 0;
                    }
                    // 下标+1,就是对应的值
                    temp = j + 1;
                    b = false;
                    break;
                }
            }
            if(b){//单位{'十','百','千','万','亿'}
                for (int j = 0; j < chArr.length; j++) {
                    if (c == chArr[j]) {
                        switch (j) {
                        case 0:
                            temp *= 10;
                            break;
                        case 1:
                            temp *= 100;
                            break;
                        case 2:
                            temp *= 1000;
                            break;
                        case 3:
                            temp *= 10000;
                            break;
                        case 4:
                            temp *= 100000000;
                            break;
                        default:
                            break;
                        }
                        count++;
                    }
                }
            }
            if (i == chineseNum.length() - 1) {//遍历到最后一个字符
                result += temp;
            }
        }
        return result;
	}
	
	/**
	 * 将数字转换为中文数字, 这里只写到了万
	 * @param intInput
	 * @return
	 */
	public static String arabicNumToChineseNum(int intInput) {
		String si = String.valueOf(intInput);
		String sd = "";
		if (si.length() == 1) {
			if (intInput == 0) {
				return sd;
			}
			sd += cnArr[intInput - 1];
			return sd;
		} else if (si.length() == 2) {
			if (si.substring(0, 1).equals("1")) {
				sd += "十";
				if (intInput % 10 == 0) {
					return sd;
				}
			}
			else
				sd += (cnArr[intInput / 10 - 1] + "十");
			sd += arabicNumToChineseNum(intInput % 10);
		} else if (si.length() == 3) {
			sd += (cnArr[intInput / 100 - 1] + "百");
			if (String.valueOf(intInput % 100).length() < 2) {
				if (intInput % 100 == 0) {
					return sd;
				}
				sd += "零";
			}
			sd += arabicNumToChineseNum(intInput % 100);
		} else if (si.length() == 4) {
			sd += (cnArr[intInput / 1000 - 1] + "千");
			if (String.valueOf(intInput % 1000).length() < 3) {
				if (intInput % 1000 == 0) {
					return sd;
				}			
				sd += "零";
			}
			sd += arabicNumToChineseNum(intInput % 1000);
		} else if (si.length() == 5) {
			sd += (cnArr[intInput / 10000 - 1] + "万");
			if (String.valueOf(intInput % 10000).length() < 4) {
				if (intInput % 10000 == 0) {
					return sd;
				}
				sd += "零";
			}
			sd += arabicNumToChineseNum(intInput % 10000);
		}
 
		return sd;
	}

	/**
	 * 判断传入的字符串是否全是汉字数字
	 * @param chineseStr
	 * @return
	 */
	public static boolean isChineseNum(String chineseStr) {
		char [] ch = chineseStr.toCharArray();
		for (char c : ch) {
			if (!allChineseNum.contains(String.valueOf(c))) {
				return false;
			}
		}
		return true;
	}
	
	/**
	 * 判断数字字符串是否是整数字符串
	 * @param str
	 * @return
	 */
	public static boolean isNum(String str) {
		String reg = "[0-9]+";
		return str.matches(reg);
	}
	
	public static void main(String[] args) {
		System.out.println(arabicNumToChineseNum(39999));
	}
}

 

  • 6
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值