/**
*
* @param str 待转换的数字字符串,如12345678.99
* @param islong true为长格式,false为短格式
* @param cnIndex 0为大写,1为小写
* @return
*/
private static String toUpper(String str, boolean islong, int cnIndex) {
if (str.indexOf(",") != -1) {
str = str.replace(",", "");
}
//TODO 这里还存在百分号有问题的情况
if (str.indexOf("%") != -1) {
str = str.replace("%", "");
str = String.valueOf(Double.parseDouble(str) / 100);
}
BigDecimal val = new BigDecimal(str);
String cd = val.toPlainString();
if (cd.indexOf(".") > 14 || (cd.indexOf(".") < 0 && cd.length() > 14)) {//整数部分大于14位
System.out.println("执行数字大小写转换时异常,需要转换的值超过支持的最大值,需要转换的值:" + str);
return "";
}
double max_number = 999999999999999.99;
String digitsArray[][] = new String[2][];
String array1[] = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
String array2[] = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
digitsArray[0] = array1;
digitsArray[1] = array2;
String radicesArray[][] = new String[2][];
String radicesArray1[] = {"", "拾", "佰", "仟"};
String radicesArray2[] = {"", "十", "百", "千"};
radicesArray[0] = radicesArray1;
radicesArray[1] = radicesArray2;
String bigRadicesArray[][] = new String[2][];
String bigRadicesArray1[] = {"", "万", "亿", "万亿", "万万亿"};
String bigRadicesArray2[] = {"", "万", "亿", "万亿", "万万亿"};
bigRadicesArray[0] = bigRadicesArray1;
bigRadicesArray[1] = bigRadicesArray2;
String decimalsArray[][] = new String[2][];
String decimalsArray1[] = {"角", "分", "整", "元", "元"};
String decimalsArray2[] = {"", "", "", "", "点"};
decimalsArray[0] = decimalsArray1;
decimalsArray[1] = decimalsArray2;
// Stringiables:
String integral; // Represent integral part of digit number.
String decimal; // Represent decimal part of digit number.
String outputCharacters; // The output result.
String[] parts;
int i = 0;
// String zeroCount;
String d;
int p;
int quotient;
int modulus;
// Validate input string:
String currencyDigits = cd.toString();
boolean negativesign = false;
if (currencyDigits.substring(0, 1).indexOf("-") != -1) {
negativesign = true;
currencyDigits = currencyDigits.substring(1);
}
if (currencyDigits.matches("/[^,.\\d]/")) {
System.out.println("传入字符串不能转换为大写");
}
if ((currencyDigits).matches("/^((\\d{1,3}(,\\d{3})*(.((\\d{3},)*\\d{1,3}))?)|(\\d+(.\\d+)?))$/")) {
System.out.println("传入字符串不符合转换格式要求");
}
// Normalize the format of input digits:
currencyDigits = currencyDigits.replaceAll(",", "");// Remove comma delimiters.
currencyDigits = currencyDigits.replace("/^0+/", ""); // Trim zeros at the beginning.
if (Double.parseDouble(currencyDigits) > max_number) {
System.out.println("需要转换的数值超过最大值");
}
if (currencyDigits.indexOf(".") != -1) {
parts = currencyDigits.split("\\.");
integral = parts[0];
decimal = parts[1];
// Cut down redundant decimal digits that are after the second.
if (decimal.length() >= 2) {
if (cnIndex != 1) {
decimal = decimal.substring(0, 2);
}
}
if (Long.parseLong(decimal) == 0) {
decimal = "";
}
} else {
integral = currencyDigits;
decimal = "";
}
String digits[] = digitsArray[cnIndex];
String radices[] = radicesArray[cnIndex];
String bigRadices[] = bigRadicesArray[cnIndex];
String decimals[] = decimalsArray[cnIndex];
int zeroCount;
// Start processing:
outputCharacters = "";
// Process integral part if it is larger than 0:
if (Long.parseLong(integral) > 0) {
zeroCount = 0;
for (int i1 = 0; i1 < integral.length(); i1++) {
p = integral.length() - i1 - 1;
d = integral.substring(i1, i1 + 1);
quotient = p / 4;
modulus = p % 4;
if (!islong) {
if ("0".equals(d)) {
zeroCount++;
} else {
if (zeroCount > 0) {
outputCharacters += digits[0];
}
zeroCount = 0;
outputCharacters += digits[Integer.parseInt(d)] + radices[modulus];
}
} else {
outputCharacters += digits[Integer.parseInt(d)] + radices[modulus];
}
if (modulus == 0
&& (outputCharacters.charAt(outputCharacters.length() - 1)) != bigRadices[2].charAt(0)) {
outputCharacters += bigRadices[quotient];
zeroCount = 0;
}
}
if (Strings.isBlank(decimal)) {
outputCharacters += decimals[3];
} else {
outputCharacters += decimals[4];
}
}else if(Long.parseLong(integral) == 0){
if (Strings.isBlank(decimal)) {
outputCharacters += digits[0];
if(cnIndex != 1){
outputCharacters += decimals[4];
}
} else {
outputCharacters += digits[0] + decimals[4];
}
}
boolean isInteger = true;
// Process decimal part if there is:
if (Strings.isNotBlank(decimal)) {
d = decimal.substring(i, 1);
for (int i1 = 0; i1 < decimal.length(); i1++) {
d = decimal.substring(i1, i1 + 1);
if (!"0".equals(d) || islong) {
outputCharacters += digits[Integer.parseInt(d)] + ((cnIndex != 1) ? decimals[i1] : "");
} else if ("0".equals(d)) {
//原始条件:&& i1 == 1 OA-109969数字字段使用【中文小写】参与动态组合,结果显示不对,数字小数位中间的"0"没有读取到。
outputCharacters += digits[0] + ((cnIndex == 1) ? decimals[i1] : "");
}
//原始条件:&& i1 == 1OA-109969数字字段使用【中文小写】参与动态组合,结果显示不对,数字小数位中间的"0"没有读取到。
if (!"0".equals(d) ) {
isInteger = false;
}
}
}
// Confirm and return the final output string:
if (Strings.isBlank(outputCharacters)) {
outputCharacters = digits[0] + decimals[3];
}
if (isInteger) {
outputCharacters += decimals[2];
}
outputCharacters = "" + outputCharacters;
if (negativesign) {
outputCharacters = "负" + outputCharacters;
}
return outputCharacters;
}