package com.zy.crm.manager.utils;
|
|
//数字中文大写工具类
|
public class ChineseNumberUtils {
|
|
private static final String[] CN_NUMERIC = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
|
|
private static final String[] CN_CARRY = {"", "拾", "佰", "仟"};
|
|
private static final String[] CN_UNITS = {"", "万", "亿", "兆"};
|
|
private static final String CN_FULL = "整";
|
|
private static final String CN_NEGATIVE = "负";
|
|
public static String numberToChinese(double number) {
|
if (number == 0) {
|
return CN_NUMERIC[0];
|
}
|
|
long temp = (long) (number * 100);
|
int numFen = (int) (temp % 10);
|
temp = temp / 10;
|
int numJiao = (int) (temp % 10);
|
temp = temp / 10;
|
int[] parts = new int[20];
|
int numParts = 0;
|
for (int i = 0; temp != 0; i++) {
|
int partNum = (int) (temp % 10000);
|
parts[i] = partNum;
|
temp = temp / 10000;
|
numParts++;
|
}
|
|
boolean lastIsZero = true;
|
String chineseStr = "";
|
for (int i = 0; i < numParts; i++) {
|
if (parts[i] == 0) {
|
lastIsZero = true;
|
if (i == numParts - 1) {
|
chineseStr = CN_UNITS[i] + chineseStr;
|
}
|
continue;
|
}
|
if (lastIsZero) {
|
chineseStr = CN_NUMERIC[0] + chineseStr;
|
}
|
String partChinese = partTranslate(parts[i]);
|
partChinese += CN_UNITS[i];
|
chineseStr = partChinese + chineseStr;
|
lastIsZero = false;
|
}
|
|
if (numFen == 0 && numJiao == 0) {
|
chineseStr += CN_FULL;
|
} else if (numFen == 0) {
|
chineseStr += CN_NUMERIC[numJiao] + "角" + CN_FULL;
|
} else {
|
if (lastIsZero) {
|
chineseStr += CN_NUMERIC[0];
|
}
|
chineseStr += CN_NUMERIC[numJiao] + "角" + CN_NUMERIC[numFen] + "分";
|
}
|
|
return chineseStr;
|
}
|
|
private static String partTranslate(int partNum) {
|
if (partNum < 0 || partNum > 10000) {
|
return "";
|
}
|
|
String[] chNum = new String[4];
|
int unitPos = 0;
|
boolean zero = true;
|
while (partNum > 0) {
|
int num = partNum % 10;
|
if (num == 0) {
|
if (!zero) {
|
zero = true;
|
chNum[unitPos++] = CN_NUMERIC[num];
|
}
|
} else {
|
zero = false;
|
chNum[unitPos++] = CN_NUMERIC[num] + CN_CARRY[unitPos - 1];
|
}
|
partNum = partNum / 10;
|
}
|
|
StringBuilder strBuf = new StringBuilder();
|
boolean hasValue = false;
|
for (int i = 0; i < 4; i++) {
|
if (chNum[i] != null) {
|
if (i > 0 && !hasValue) {
|
strBuf.insert(0, CN_NUMERIC[0]);
|
}
|
strBuf.insert(0, chNum[i]);
|
hasValue = true;
|
}
|
}
|
|
return strBuf.toString();
|
}
|
|
public static void main(String[] args) {
|
double number = 123456789.23;
|
String chineseNumber = numberToChinese(number);
|
System.out.println(chineseNumber);
|
}
|
|
}
|