package com.springboot.entity.utils;
import lombok.extern.slf4j.Slf4j;
/**
* @author maniac
* date:2019/07/14 09:56
* @do 数字转换处理工具类
*/
@Slf4j
public class NumberTransformUtil {
//阿拉伯数字转中文小写,基础数值
private static final String[] BASE_LOW = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
//阿拉伯数字转中文小写,数字权重位
private static final String[] UNIT_LOW = {"", "十", "百", "千", "万", "亿"};
//阿拉伯数字转中文大写,基础数值
private static final String[] BASE_CAP = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
//阿拉伯数字转中文大写,数字权重位
private static final String[] UNIT_CAP = {"", "拾", "佰", "仟", "万", "亿"};
/**
* 转换阿拉伯数字为中文小写(int:0-2147483647)
*
* @param number 数字字符串
* @return 转换后的中文小写字符串 空串为发生转换异常
*/
public static String toLowercaseCh(String number) {
return toChineseNumber(number, BASE_LOW, UNIT_LOW);
}
/**
* 转换阿拉伯数字为中文大写(int:0-2147483647)
*
* @param number 数字字符串
* @return 转换后的中文大写字符串 空串为发生转换异常
*/
public static String toCapitalCh(String number) {
return toChineseNumber(number, BASE_CAP, UNIT_CAP);
}
/**
* 转换阿拉伯数字为中文小写(int:0-99)
*
* @param number 正整数
* @return 中文小写字符串
*/
public static String toLowercaseChWithinHundred(int number) {
if (number < 0) {
return BASE_LOW[0];
} else if (number <= 9) {
return BASE_LOW[number];
} else {
return BASE_LOW[number / 10] + UNIT_LOW[1] + BASE_LOW[number % 10];
}
}
/**
* 转换阿拉伯数字为中文数字
*
* @param number 数字字符串
* @param baseNum 基础数值集合
* @param unitNum 数字权重位集合
* @return 转换后的中文字符串 空串为发生转换异常
*/
private static String toChineseNumber(String number, String[] baseNum, String[] unitNum) {
int num;
try {
num = Integer.parseInt(number);
} catch (Exception e) {
log.info("【数据转换操作】字符串转Int类型出现异常,字符串:{},e:", number, e);
return "";
}
if (num < 0) {
return baseNum[0];
} else if (num <= 9) {
return baseNum[num];
}
if (number.length() <= 4) {
return dealNumberFragment(number, baseNum, unitNum);
} else if (number.length() <= 8) {
String fragmentOne = number.substring(number.length() - 4);
String resultOne = dealNumberFragment(fragmentOne, baseNum, unitNum);
String fragmentTwo = number.substring(0, number.length() - 4);
String resultTwo = dealNumberFragment(fragmentTwo, baseNum, unitNum);
return resultTwo + unitNum[4] + resultOne;
} else {//int 最长为10位数字字符串
String fragmentOne = number.substring(number.length() - 4);
String resultOne = dealNumberFragment(fragmentOne, baseNum, unitNum);
String fragmentTwo = number.substring(number.length() - 8, number.length() - 4);
String resultTwo =
if (!"0000".equals(fragmentTwo)) {//判断是否追加万字
resultTwo = dealNumberFragment(fragmentTwo, baseNum, unitNum)+ unitNum[4];
}else{
if (!"0000".equals(fragmentOne)) {//权重万 为0000 但是0-9999 不为0000 则需补零
resultTwo = baseNum[0];
}
}
String fragmentThree = number.substring(0, number.length() - 8);
String resultThree = dealNumberFragment(fragmentThree, baseNum, unitNum);
return resultThree + unitNum[5] + resultTwo + unitNum[4] + resultOne;
}
}
/**
* 转换阿拉伯数字为中文小写(int:0-9999)
*
* @param number 数字字符串
* @param baseNum 基础数字数组
* @param unitNum 权重数组
* @return 中文字符串
*/
private static String dealNumberFragment(String number, String[] baseNum, String[] unitNum) {
StringBuilder result = new StringBuilder();
int dealNum = Integer.parseInt(number);
boolean preFlag = false;//标识上一个余数是否为0 true 为0 false 不为0
for (int i = number.length() - 1; i >= 0; i--) {
int remainder = dealNum % 10;
dealNum = dealNum / 10;
if (0 == remainder) {//用于处理连续为0的情况
preFlag = true;
} else {
if (preFlag) {//上一个是0 当前不是0
result.insert(0, baseNum[0]);
result.insert(0, baseNum[remainder] + unitNum[number.length() - 1 - i]);
preFlag = false;
} else {
result.insert(0, baseNum[remainder] + unitNum[number.length() - 1 - i]);
preFlag = false;
}
}
}
//补全首位的零 补零操作需放在去零操作的前面解决处理 “0000”情况
if (number.startsWith("0")) {
result.insert(0, baseNum[0]);
}
//去掉末尾的零
if (number.endsWith("0")) {
result.deleteCharAt(result.length() - 1);
}
return result.toString();
}
private NumberTransformUtil() {
}
}