/**
*
*/
package com.mjp.core.rmb;
/**
* @author mojianpo
*
*/
public class RmbUtil {
/**
* 将数字转换为对应的大写字母列如:0--零
*
* @param num
* @return
*/
private static char tochina(int num) {
char result = '零';
switch (num) {
case 0:
result = '零';
break;
case 1:
result = '壹';
break;
case 2:
result = '贰';
break;
case 3:
result = '叁';
break;
case 4:
result = '肆';
break;
case 5:
result = '伍';
break;
case 6:
result = '陆';
break;
case 7:
result = '柒';
break;
case 8:
result = '捌';
break;
case 9:
result = '玖';
break;
}
return result;
}
/**
* 将数字转换成大写人民币,如果不是数字,则返回零,其中最大数值999999999999.99,无负数,只保留到分上
*/
public static String numToRMB(String str) {
String result = "";
if (str.charAt(0) == '-') {
str = str.substring(1);
}
if (!isNumber(str)) {
return "零";
}
char[] strtmp = new char[30];
double temp = Double.parseDouble(str);
if (str.substring(str.indexOf(".") + 1).trim().length() < 3) {
temp = temp + 0.001;
}
// System.out.println(temp);
if (temp > 1000000000000.1 || temp < 0.00) {
return "零";
}
long re = (long) (temp * 100);
// System.out.println(re);
String zheng = (re % 10 == 0 && re % 100 == 0) ? "整" : "";
strtmp[1] = '零';
strtmp[2] = '仟';
strtmp[3] = '零';
strtmp[4] = '佰';
strtmp[5] = '零';
strtmp[6] = '拾';
strtmp[7] = '零';
strtmp[8] = '亿';
strtmp[9] = '零';
strtmp[10] = '仟';
strtmp[11] = '零';
strtmp[12] = '佰';
strtmp[13] = '零';
strtmp[14] = '拾';
strtmp[15] = '零';
strtmp[16] = '万';
strtmp[17] = '零';
strtmp[18] = '仟';
strtmp[19] = '零';
strtmp[20] = '佰';
strtmp[21] = '零';
strtmp[22] = '拾';
strtmp[23] = '零';
strtmp[24] = '元';
strtmp[25] = '零';
strtmp[26] = '角';
strtmp[27] = '零';
strtmp[28] = '分';
strtmp[29] = '整';
int i;
for (i = 1; i < 15; i++) {
if ((re % 10) != 0) {
strtmp[29 - i * 2] = tochina((int) (re % 10));
}
re = (long) (re / 10);
if (re == 0) {
break;
}
}
i = 29 - i * 2;
boolean flag = false;// 标志零
while (i < 29) {
if (strtmp[i] == '零') {
i++;
flag = true;
if (strtmp[i] == '万' || strtmp[i] == '圆' || strtmp[i] == '亿') {
result = result + strtmp[i];
if (result.indexOf("亿万") > -1) {
result = result.replaceAll("亿万", "亿");
}
}
} else {
if (flag == true) {
flag = false;
result = result + "零";
}
result = result + strtmp[i];
i++;
result = result + strtmp[i];
}
i++;
}
result = result + zheng;
return result;
}
/**
* 判断字符串是否是数字,(可以有符号,也可有且最多只有一个点)
*/
private static boolean isNumber(String s) {
s = s.trim();
boolean pointfirsttime = true;
boolean haveDigit = false;
int i = 0;
if (s == null || s.length() < 1) {
return false;
}
if (s.charAt(0) == '-') {
i++;
}
while (i < s.length()) {
if (!Character.isDigit(s.charAt(i))) {
if ('.' == s.charAt(i) && pointfirsttime) {
pointfirsttime = false;
} else {
return false;
}
} else {
haveDigit = true;
}
i++;
}
if (!haveDigit) {
return false;
}
return true;
}
/**
* 将大写人民币转换成数字
*/
public static String rmbToNUM(String str) {
double temp1, temp2;
double result = 0;
for (int i = 0; i < str.length(); i++) {
temp1 = toNumber(str.charAt(i));
if (temp1 == 0) {
continue;
}
temp2 = toNumber(str.charAt(++i));
if (temp2 == 100000000) {
result = result * temp2;
} else if (temp2 == 10000) {
result = ((long) (result / 100000000)) * 100000000
+ (((long) result) % 100000000) * temp2;
}
result = result + temp1 * temp2;
}
return String.valueOf(result);
}
/**
* 将大写字母转换成对应的数字
*/
private static double toNumber(char c) {
double temp = 0;
switch (c) {
case '零':
temp = 0;
break;
case '壹':
temp = 1;
break;
case '贰':
temp = 2;
break;
case '叁':
temp = 3;
break;
case '肆':
temp = 4;
break;
case '伍':
temp = 5;
break;
case '陆':
temp = 6;
break;
case '柒':
temp = 7;
break;
case '捌':
temp = 8;
break;
case '玖':
temp = 9;
break;
case '拾':
temp = 10;
break;
case '佰':
temp = 100;
break;
case '仟':
temp = 1000;
break;
case '万':
temp = 10000;
break;
case '亿':
temp = 100000000;
break;
case '整':
temp = 0;
break;
case '元':
temp = 1;
break;
case '角':
temp = 0.1;
break;
case '分':
temp = 0.01;
break;
}
return temp;
}
/**
* 测试方法是否正确
*/
public static void main(String[] arg) {
String temp = "121.00";
System.out.println(temp);
String re = RmbUtil.numToRMB(temp);
System.out.println(re);
re = RmbUtil.rmbToNUM(re);
System.out.println(re);
double dd = Double.parseDouble(re);
System.out.println(dd);
}
}
*
*/
package com.mjp.core.rmb;
/**
* @author mojianpo
*
*/
public class RmbUtil {
/**
* 将数字转换为对应的大写字母列如:0--零
*
* @param num
* @return
*/
private static char tochina(int num) {
char result = '零';
switch (num) {
case 0:
result = '零';
break;
case 1:
result = '壹';
break;
case 2:
result = '贰';
break;
case 3:
result = '叁';
break;
case 4:
result = '肆';
break;
case 5:
result = '伍';
break;
case 6:
result = '陆';
break;
case 7:
result = '柒';
break;
case 8:
result = '捌';
break;
case 9:
result = '玖';
break;
}
return result;
}
/**
* 将数字转换成大写人民币,如果不是数字,则返回零,其中最大数值999999999999.99,无负数,只保留到分上
*/
public static String numToRMB(String str) {
String result = "";
if (str.charAt(0) == '-') {
str = str.substring(1);
}
if (!isNumber(str)) {
return "零";
}
char[] strtmp = new char[30];
double temp = Double.parseDouble(str);
if (str.substring(str.indexOf(".") + 1).trim().length() < 3) {
temp = temp + 0.001;
}
// System.out.println(temp);
if (temp > 1000000000000.1 || temp < 0.00) {
return "零";
}
long re = (long) (temp * 100);
// System.out.println(re);
String zheng = (re % 10 == 0 && re % 100 == 0) ? "整" : "";
strtmp[1] = '零';
strtmp[2] = '仟';
strtmp[3] = '零';
strtmp[4] = '佰';
strtmp[5] = '零';
strtmp[6] = '拾';
strtmp[7] = '零';
strtmp[8] = '亿';
strtmp[9] = '零';
strtmp[10] = '仟';
strtmp[11] = '零';
strtmp[12] = '佰';
strtmp[13] = '零';
strtmp[14] = '拾';
strtmp[15] = '零';
strtmp[16] = '万';
strtmp[17] = '零';
strtmp[18] = '仟';
strtmp[19] = '零';
strtmp[20] = '佰';
strtmp[21] = '零';
strtmp[22] = '拾';
strtmp[23] = '零';
strtmp[24] = '元';
strtmp[25] = '零';
strtmp[26] = '角';
strtmp[27] = '零';
strtmp[28] = '分';
strtmp[29] = '整';
int i;
for (i = 1; i < 15; i++) {
if ((re % 10) != 0) {
strtmp[29 - i * 2] = tochina((int) (re % 10));
}
re = (long) (re / 10);
if (re == 0) {
break;
}
}
i = 29 - i * 2;
boolean flag = false;// 标志零
while (i < 29) {
if (strtmp[i] == '零') {
i++;
flag = true;
if (strtmp[i] == '万' || strtmp[i] == '圆' || strtmp[i] == '亿') {
result = result + strtmp[i];
if (result.indexOf("亿万") > -1) {
result = result.replaceAll("亿万", "亿");
}
}
} else {
if (flag == true) {
flag = false;
result = result + "零";
}
result = result + strtmp[i];
i++;
result = result + strtmp[i];
}
i++;
}
result = result + zheng;
return result;
}
/**
* 判断字符串是否是数字,(可以有符号,也可有且最多只有一个点)
*/
private static boolean isNumber(String s) {
s = s.trim();
boolean pointfirsttime = true;
boolean haveDigit = false;
int i = 0;
if (s == null || s.length() < 1) {
return false;
}
if (s.charAt(0) == '-') {
i++;
}
while (i < s.length()) {
if (!Character.isDigit(s.charAt(i))) {
if ('.' == s.charAt(i) && pointfirsttime) {
pointfirsttime = false;
} else {
return false;
}
} else {
haveDigit = true;
}
i++;
}
if (!haveDigit) {
return false;
}
return true;
}
/**
* 将大写人民币转换成数字
*/
public static String rmbToNUM(String str) {
double temp1, temp2;
double result = 0;
for (int i = 0; i < str.length(); i++) {
temp1 = toNumber(str.charAt(i));
if (temp1 == 0) {
continue;
}
temp2 = toNumber(str.charAt(++i));
if (temp2 == 100000000) {
result = result * temp2;
} else if (temp2 == 10000) {
result = ((long) (result / 100000000)) * 100000000
+ (((long) result) % 100000000) * temp2;
}
result = result + temp1 * temp2;
}
return String.valueOf(result);
}
/**
* 将大写字母转换成对应的数字
*/
private static double toNumber(char c) {
double temp = 0;
switch (c) {
case '零':
temp = 0;
break;
case '壹':
temp = 1;
break;
case '贰':
temp = 2;
break;
case '叁':
temp = 3;
break;
case '肆':
temp = 4;
break;
case '伍':
temp = 5;
break;
case '陆':
temp = 6;
break;
case '柒':
temp = 7;
break;
case '捌':
temp = 8;
break;
case '玖':
temp = 9;
break;
case '拾':
temp = 10;
break;
case '佰':
temp = 100;
break;
case '仟':
temp = 1000;
break;
case '万':
temp = 10000;
break;
case '亿':
temp = 100000000;
break;
case '整':
temp = 0;
break;
case '元':
temp = 1;
break;
case '角':
temp = 0.1;
break;
case '分':
temp = 0.01;
break;
}
return temp;
}
/**
* 测试方法是否正确
*/
public static void main(String[] arg) {
String temp = "121.00";
System.out.println(temp);
String re = RmbUtil.numToRMB(temp);
System.out.println(re);
re = RmbUtil.rmbToNUM(re);
System.out.println(re);
double dd = Double.parseDouble(re);
System.out.println(dd);
}
}