|
@@ -1,10 +1,6 @@
|
|
|
package com.minpay.common.util;
|
|
|
|
|
|
-import java.text.DecimalFormat;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.Map;
|
|
|
-
|
|
|
-import com.minpay.common.util.StringHelper;
|
|
|
+import java.math.BigDecimal;
|
|
|
|
|
|
/**
|
|
|
* @Package:cn.ucaner.framework.utils
|
|
@@ -18,186 +14,122 @@ import com.minpay.common.util.StringHelper;
|
|
|
* @version V1.1
|
|
|
*/
|
|
|
public class ChineseNumber {
|
|
|
-
|
|
|
- private static final String[] BEFORE_SCALE = { "万", "仟", "佰", "拾", "亿", "仟", "佰", "拾", "万", "仟", "佰", "拾", "" };
|
|
|
-
|
|
|
- private static final String[] AFTER_SCALE = { "角", "分" };
|
|
|
-
|
|
|
- private static final String DEFAULT_PATH_SEPARATOR = ".";
|
|
|
-
|
|
|
- private static final Map<String, String> NUMBER_MAPPING = new HashMap<String, String>();
|
|
|
- static {
|
|
|
- NUMBER_MAPPING.put("0", "零");
|
|
|
- NUMBER_MAPPING.put("1", "壹");
|
|
|
- NUMBER_MAPPING.put("2", "贰");
|
|
|
- NUMBER_MAPPING.put("3", "叁");
|
|
|
- NUMBER_MAPPING.put("4", "肆");
|
|
|
- NUMBER_MAPPING.put("5", "伍");
|
|
|
- NUMBER_MAPPING.put("6", "陆");
|
|
|
- NUMBER_MAPPING.put("7", "柒");
|
|
|
- NUMBER_MAPPING.put("8", "捌");
|
|
|
- NUMBER_MAPPING.put("9", "玖");
|
|
|
- }
|
|
|
-
|
|
|
- public static String getChineseNumber(String number) {
|
|
|
- return getChineseNumber(number, null, null);
|
|
|
- }
|
|
|
-
|
|
|
- public static String getChineseNumber(String number, String unit, String postfix) {
|
|
|
-
|
|
|
- String[] numbers = StringHelper.strToStrArray(number, DEFAULT_PATH_SEPARATOR);
|
|
|
- if (numbers.length > 2) {
|
|
|
- new NumberFormatException("数字格式错误!");
|
|
|
+ /**
|
|
|
+ * 汉语中数字大写
|
|
|
+ */
|
|
|
+ private static final String[] CN_UPPER_NUMBER = { "零", "壹", "贰", "叁", "肆",
|
|
|
+ "伍", "陆", "柒", "捌", "玖" };
|
|
|
+ /**
|
|
|
+ * 汉语中货币单位大写,这样的设计类似于占位符
|
|
|
+ */
|
|
|
+ private static final String[] CN_UPPER_MONETRAY_UNIT = { "分", "角", "元",
|
|
|
+ "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "兆", "拾",
|
|
|
+ "佰", "仟" };
|
|
|
+ /**
|
|
|
+ * 特殊字符:整
|
|
|
+ */
|
|
|
+ private static final String CN_FULL = "整";
|
|
|
+ /**
|
|
|
+ * 特殊字符:负
|
|
|
+ */
|
|
|
+ private static final String CN_NEGATIVE = "负";
|
|
|
+ /**
|
|
|
+ * 金额的精度,默认值为2
|
|
|
+ */
|
|
|
+ private static final int MONEY_PRECISION = 2;
|
|
|
+ /**
|
|
|
+ * 特殊字符:零元整
|
|
|
+ */
|
|
|
+ private static final String CN_ZEOR_FULL = "零元" + CN_FULL;
|
|
|
+
|
|
|
+ public static String getChineseNumber(String num){
|
|
|
+ return getChineseNumber(new BigDecimal(num));
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 把输入的金额转换为汉语中人民币的大写
|
|
|
+ *
|
|
|
+ * @param numberOfMoney
|
|
|
+ * 输入的金额
|
|
|
+ * @return 对应的汉语大写
|
|
|
+ */
|
|
|
+ public static String getChineseNumber(BigDecimal numberOfMoney) {
|
|
|
+ StringBuffer sb = new StringBuffer();
|
|
|
+ // -1, 0, or 1 as the value of this BigDecimal is negative, zero, or
|
|
|
+ // positive.
|
|
|
+ int signum = numberOfMoney.signum();
|
|
|
+ // 零元整的情况
|
|
|
+ if (signum == 0) {
|
|
|
+ return CN_ZEOR_FULL;
|
|
|
}
|
|
|
- int length = numbers[0].length();
|
|
|
- int isZero = 0;
|
|
|
- StringBuffer result = new StringBuffer();
|
|
|
-
|
|
|
- for (int i = 0; i < length; i++) {
|
|
|
- String digit = CommonUtil.objToString(numbers[0].charAt(i));
|
|
|
-
|
|
|
- boolean allZero = true; // 如果后继的全部是零,则跳出
|
|
|
- for (int j = i; j < length; j++) {
|
|
|
- if (numbers[0].charAt(j) != '0') {
|
|
|
- allZero = false;
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if (allZero) {
|
|
|
- boolean hasValue = false;
|
|
|
- for (int z = i; z >= 0; z--) {
|
|
|
- if (numbers[0].charAt(z) != '0' && length - z <= 7 && length - z >= 5) {
|
|
|
- hasValue = true;
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- // 加万单位
|
|
|
- if ( ( length - i > 4 && length <= 8 ) || ( hasValue && length - i > 4 )) {
|
|
|
- result.append(BEFORE_SCALE[8]);
|
|
|
- }
|
|
|
- // 加亿单位
|
|
|
- if (length - i >= 9) {
|
|
|
- result.append(BEFORE_SCALE[4]);
|
|
|
- }
|
|
|
+ // 这里会进行金额的四舍五入
|
|
|
+ long number = numberOfMoney.movePointRight(MONEY_PRECISION)
|
|
|
+ .setScale(0, 4).abs().longValue();
|
|
|
+ // 得到小数点后两位值
|
|
|
+ long scale = number % 100;
|
|
|
+ int numUnit = 0;
|
|
|
+ int numIndex = 0;
|
|
|
+ boolean getZero = false;
|
|
|
+ // 判断最后两位数,一共有四中情况:00 = 0, 01 = 1, 10, 11
|
|
|
+ if (!(scale > 0)) {
|
|
|
+ numIndex = 2;
|
|
|
+ number = number / 100;
|
|
|
+ getZero = true;
|
|
|
+ }
|
|
|
+ if ((scale > 0) && (!(scale % 10 > 0))) {
|
|
|
+ numIndex = 1;
|
|
|
+ number = number / 10;
|
|
|
+ getZero = true;
|
|
|
+ }
|
|
|
+ int zeroSize = 0;
|
|
|
+ while (true) {
|
|
|
+ if (number <= 0) {
|
|
|
break;
|
|
|
}
|
|
|
-
|
|
|
- if (length < 9 && length - i == 5) {
|
|
|
- if (!"0".equals(digit) && isZero > 0) {
|
|
|
- result.append(NUMBER_MAPPING.get("0"));
|
|
|
+ // 每次获取到最后一个数
|
|
|
+ numUnit = (int) (number % 10);
|
|
|
+ if (numUnit > 0) {
|
|
|
+ if ((numIndex == 9) && (zeroSize >= 3)) {
|
|
|
+ sb.insert(0, CN_UPPER_MONETRAY_UNIT[6]);
|
|
|
}
|
|
|
- if ("0".equals(digit)) {
|
|
|
- result.append(BEFORE_SCALE[8]);
|
|
|
- if (isZero > 0) {
|
|
|
- result.append(NUMBER_MAPPING.get("0"));
|
|
|
- }
|
|
|
- continue;
|
|
|
+ if ((numIndex == 13) && (zeroSize >= 3)) {
|
|
|
+ sb.insert(0, CN_UPPER_MONETRAY_UNIT[10]);
|
|
|
}
|
|
|
- }
|
|
|
- if ("0".equals(digit) && length > 9 && length - i == 9) {
|
|
|
- result.append(BEFORE_SCALE[4]);
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- if (isZero < 1 || !"0".equals(digit)) {
|
|
|
- if ("0".equals(digit)) {
|
|
|
- if (length - i != 6 && length - i != 7) {
|
|
|
- result.append(NUMBER_MAPPING.get(digit));
|
|
|
- }
|
|
|
- } else {
|
|
|
- result.append(NUMBER_MAPPING.get(digit));
|
|
|
+ sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
|
|
|
+ sb.insert(0, CN_UPPER_NUMBER[numUnit]);
|
|
|
+ getZero = false;
|
|
|
+ zeroSize = 0;
|
|
|
+ } else {
|
|
|
+ ++zeroSize;
|
|
|
+ if (!(getZero)) {
|
|
|
+ sb.insert(0, CN_UPPER_NUMBER[numUnit]);
|
|
|
}
|
|
|
-
|
|
|
- if (!"0".equals(digit)) {
|
|
|
- result.append(BEFORE_SCALE[BEFORE_SCALE.length - length + i]);
|
|
|
+ if (numIndex == 2) {
|
|
|
+ if (number > 0) {
|
|
|
+ sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
|
|
|
+ }
|
|
|
+ } else if (((numIndex - 2) % 4 == 0) && (number % 1000 > 0)) {
|
|
|
+ sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
|
|
|
}
|
|
|
+ getZero = true;
|
|
|
}
|
|
|
-
|
|
|
- if ("0".equals(digit)) {
|
|
|
- isZero++;
|
|
|
- } else {
|
|
|
- isZero = 0;
|
|
|
- }
|
|
|
+ // 让number每次都去掉最后一个数
|
|
|
+ number = number / 10;
|
|
|
+ ++numIndex;
|
|
|
}
|
|
|
- result.append(unit == null ? "圆" : result.append(unit));
|
|
|
-
|
|
|
- if (numbers.length == 1) {
|
|
|
- result.append(postfix == null ? "整" : result.append(postfix));
|
|
|
- return result.toString();
|
|
|
+ // 如果signum == -1,则说明输入的数字为负数,就在最前面追加特殊字符:负
|
|
|
+ if (signum == -1) {
|
|
|
+ sb.insert(0, CN_NEGATIVE);
|
|
|
}
|
|
|
-
|
|
|
- length = numbers[1].length();
|
|
|
- for (int j = 0; j < length; j++) {
|
|
|
- if (j > 2) {
|
|
|
- break;
|
|
|
- }
|
|
|
- if (numbers[1].charAt(j) == '0') {
|
|
|
- continue;
|
|
|
- }
|
|
|
- result.append(NUMBER_MAPPING.get(CommonUtil.objToString(numbers[1].charAt(j))));
|
|
|
- result.append(AFTER_SCALE[j]);
|
|
|
+ // 输入的数字小数点后两位为"00"的情况,则要在最后追加特殊字符:整
|
|
|
+ if (!(scale > 0)) {
|
|
|
+ sb.append(CN_FULL);
|
|
|
}
|
|
|
-
|
|
|
- result.append(postfix == null ? "整" : result.append(postfix));
|
|
|
-
|
|
|
- return result.toString();
|
|
|
- }
|
|
|
-
|
|
|
- public static String getChineseNumber(int number) {
|
|
|
- return getChineseNumber(new Integer(number));
|
|
|
- }
|
|
|
-
|
|
|
- public static String getChineseNumber(int number, String unit, String postfix) {
|
|
|
- return getChineseNumber(new Integer(number), unit, postfix);
|
|
|
- }
|
|
|
-
|
|
|
- public static String getChineseNumber(Long number) {
|
|
|
- return getChineseNumber(number.toString(), null, null);
|
|
|
- }
|
|
|
-
|
|
|
- public static String getChineseNumber(Integer number) {
|
|
|
- return getChineseNumber(number.toString(), null, null);
|
|
|
- }
|
|
|
-
|
|
|
- public static String getChineseNumber(Integer number, String unit, String postfix) {
|
|
|
- return getChineseNumber(number.toString(), unit, postfix);
|
|
|
+ return sb.toString();
|
|
|
}
|
|
|
-
|
|
|
- public static String getChineseNumber(Long number, String unit, String postfix) {
|
|
|
- return getChineseNumber(number.toString(), unit, postfix);
|
|
|
- }
|
|
|
-
|
|
|
- public static String getChineseNumber(long number) {
|
|
|
- return getChineseNumber(new Long(number));
|
|
|
- }
|
|
|
-
|
|
|
- public static String getChineseNumber(long number, String unit, String postfix) {
|
|
|
- return getChineseNumber(new Long(number), unit, postfix);
|
|
|
- }
|
|
|
-
|
|
|
- public static String getChineseNumber(double number, String unit, String postfix) {
|
|
|
- DecimalFormat f = (DecimalFormat) DecimalFormat.getInstance();
|
|
|
- f.applyLocalizedPattern("#.##");
|
|
|
- return getChineseNumber(f.format(number), unit, postfix);
|
|
|
- }
|
|
|
-
|
|
|
- public static String getChineseNumber(double number) {
|
|
|
- return getChineseNumber(number, null, null);
|
|
|
- }
|
|
|
-
|
|
|
- public static String getChineseNumber(Double number) {
|
|
|
- return getChineseNumber(number.doubleValue());
|
|
|
- }
|
|
|
-
|
|
|
- public static String getChineseNumber(Double number, String unit, String postfix) {
|
|
|
- return getChineseNumber(number.doubleValue(), unit, postfix);
|
|
|
- }
|
|
|
-
|
|
|
+
|
|
|
public static void main(String[] args) {
|
|
|
- System.out.println(getChineseNumber(1994));
|
|
|
- System.out.println(getChineseNumber(1994.1115));
|
|
|
- System.out.println(getChineseNumber(19941115));
|
|
|
+ String s = getChineseNumber("3521.00");
|
|
|
+ System.out.println(s);
|
|
|
}
|
|
|
|
|
|
}
|