public class ConvertNumUtil {
/**
* create by Lee
*
* @param doc 文档内容
* @param minute 是否开启详情版
* @return
*/
static String CHN_NUMBER[] = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
static String CHN_UNIT[] = {"", "拾", "佰", "千"};
static String CHN_UNIT_SECTION[] = {"", "万", "亿", "万亿"};
private static String convertDate(String str){
if(CHN_NUMBER[0].equals(str.charAt(0))){
str = CHN_NUMBER[2]+str;
}
if(str.contains("年")){
str.replace("年","");
}
if(str.contains("月")){
str.replace("月","");
}
if(str.contains("日")){
str.replace("日","");
}
if(str.contains("拾")){
}
for (int i = 0; i < str.length(); i++) {
}
return null;
}
/**
* 阿拉伯数字转换为中文数字的核心算法实现。
*
* @param num 为需要转换为中文数字的阿拉伯数字,是无符号的整形数
* @return
*/
private static String NumberToChn(int num) {
StringBuffer returnStr = new StringBuffer();
Boolean needZero = false;
int pos = 0;
if (num == 0) {
returnStr.insert(0, CHN_NUMBER[0]);
}
while (num > 0) {
int section = num % 10000;
if (needZero) {
returnStr.insert(0, CHN_NUMBER[0]);
}
String sectionToChn = SectionNumToChn(section);
sectionToChn += (section != 0) ? CHN_UNIT_SECTION[pos] : CHN_UNIT_SECTION[0];
returnStr.insert(0, sectionToChn);
needZero = ((section < 1000 && section > 0) ? true : false);
pos++;
num = num / 10000;
}
return returnStr.toString();
}
/**
* 将四位的section转换为中文数字
*
* @param section
* @return
*/
private static String SectionNumToChn(int section) {
StringBuffer returnStr = new StringBuffer();
int unitPos = 0;
Boolean zero = true;
while (section > 0) {
int v = (section % 10);
if (v == 0) {
if ((section == 0) || !zero) {
zero = true;
returnStr.insert(0, CHN_NUMBER[v]);
}
} else {
zero = false;
StringBuffer tempStr = new StringBuffer(CHN_NUMBER[v]);
tempStr.append(CHN_UNIT[unitPos]);
returnStr.insert(0, tempStr);
}
unitPos++;
section = section / 10;
}
return returnStr.toString();
}
/**
* 中文转换成阿拉伯数字,中文字符串除了包括0-9的中文汉字,还包括十,百,千,万等权位。
* 此处是完成对这些权位的类型定义。
* name是指这些权位的汉字字符串。
* value是指权位多对应的数值的大小。诸如:十对应的值的大小为10,百对应为100等
* secUnit若为true,代表该权位为节权位,即万,亿,万亿等
*/
private static class Chn_Name_value {
String name;
double value;
Boolean secUnit;
private Chn_Name_value(String name, double value, Boolean secUnit) {
this.name = name;
this.value = value;
this.secUnit = secUnit;
}
}
static Chn_Name_value chnNameValue[] = {
new Chn_Name_value("拾", 10, false),
new Chn_Name_value("抬", 10, false),
new Chn_Name_value("十", 10, false),
new Chn_Name_value("佰", 100, false),
new Chn_Name_value("百", 100, false),
new Chn_Name_value("白", 100, false),
new Chn_Name_value("千", 1000, false),
new Chn_Name_value("干", 1000, false),
new Chn_Name_value("仟", 1000, false),
new Chn_Name_value("万", 10000, true),
new Chn_Name_value("亿", 100000000, true),
new Chn_Name_value("角", 0.1, false),
new Chn_Name_value("甬", 0.1, false),
new Chn_Name_value("甪", 0.1, false),
new Chn_Name_value("分", 0.01, false),
new Chn_Name_value("份", 0.01, false),
new Chn_Name_value("元", 1, false),
new Chn_Name_value("园", 1, false),
new Chn_Name_value("圆", 1, false),
};
/**
* 返回中文数字汉字所对应的阿拉伯数字,若str不为中文数字,则返回-1
*
* @param str
* @return
*/
private static int ChnNumToValue(String str) {
for (int i = 0; i < CHN_NUMBER.length; i++) {
if (str.equals(CHN_NUMBER[i])) {
return i;
}
}
return -1;
}
/**
* 返回中文汉字权位在chnNameValue数组中所对应的索引号,若不为中文汉字权位,则返回-1
*
* @param str
* @return
*/
private static double ChnUnitToValue(String str) {
for (int i = 0; i < chnNameValue.length; i++) {
if (str.equals(chnNameValue[i].name)) {
return i;
}
}
return -1;
}
/**
* 返回中文数字字符串所对应的int类型的阿拉伯数字
*
* @param str
* @return
*/
public static double ChnStringToNumber(String str) {
if(str.contains("整")){
str = str.replace("整","");
}
if(str.contains("拾") && (str.indexOf("拾") == 0)){
str = "壹"+str;
}
double returnNumber = 0;
double section = 0;
int pos = 0;
int number = 0;
while (pos < str.length()) {
int num = ChnNumToValue(str.substring(pos, pos + 1));
if (num >= 0) {
number = num;
pos++;
if (pos >= str.length()) {
section += number;
returnNumber += section;
break;
}
} else {
double chnNameValueIndex = ChnUnitToValue(str.substring(pos, pos + 1));
if (chnNameValue[(int)chnNameValueIndex].secUnit) {
section = (section + number) * chnNameValue[(int)chnNameValueIndex].value;
returnNumber += section;
section = 0;
} else {
section += number * chnNameValue[(int)chnNameValueIndex].value;
}
pos++;
number = 0;
if (pos >= str.length()) {
returnNumber += section;
break;
}
}
}
return (double) Math.round(returnNumber*100)/100;
}
}