/**
* 将浮点数转换成人民币读法字符串
* @author joy
*
*/
public class NumToRmb {
private static char []numStr
= {'零', '壹','贰','叁','肆','伍','陆','柒','捌','玖','拾'};
private String num;
NumToRmb (String num) {
this.num = num;
}
/**
* 整数位只支持12位以内,即最大识别范围为千亿
* @return
*/
public String numToRmb() {
//去掉分隔的逗号
String tmp = num.replaceAll("\\,", "");
char []numArray = tmp.toCharArray();
int lenInt = 0;
String out = null;
lenInt = tmp.indexOf('.');
if (lenInt == -1) {
lenInt = tmp.length();
}
if (lenInt >= 13) {
System.out.println("此方法只能识别13位以内的数字,即最大支持范围为千亿");
return null;
}
if (lenInt <= 4) {
out = printRmb4(numArray, 0, lenInt);
}
if (lenInt <= 8 && lenInt > 4) {
out = printRmb8(numArray, 0, lenInt);
}
if (lenInt <= 12 && lenInt > 8) {
out = printRmb12(numArray, 0, lenInt);
}
out += printFlt(numArray, lenInt+1, numArray.length);
return out;
}
private String printRmb4(char []array, int start, int end) {
StringBuilder out = new StringBuilder();
int len = end - start;
char []rmb = {'千', '百', '十', '元'};
for (int i = start; i < end; i++) {
if (array[i] != '0') {
out.append(numStr[Integer.parseInt(array[i] +"")]);
out.append(rmb[4-len+i-start]);
} else {
if (i > start && array[i-1] != '0') {
out.append("零");
}
}
}
return out.toString();
}
private String printRmb8(char []array, int start, int end) {
StringBuilder out = new StringBuilder();
int len = end - start - 4;
char []rmb = {'千', '百', '十', '万'};
for (int i = start; i < end - 4; i++) {
if (array[i] != '0') {
out.append(numStr[Integer.parseInt(array[i] +"")]);
out.append(rmb[4-len+i-start]);
} else {
if (i > start && array[i-1] != '0') {
out.append("零");
}
}
}
out.append(printRmb4(array, end - 4, end));
return out.toString();
}
private String printRmb12(char []array, int start, int end) {
StringBuilder out = new StringBuilder();
int len = end - start - 8;
char []rmb = {'千', '百', '十', '亿'};
for (int i = start; i < end - 8; i++) {
if (array[i] != '0') {
out.append(numStr[Integer.parseInt(array[i] +"")]);
out.append(rmb[4-len+i-start]);
} else {
if (i > start && array[i-1] != '0') {
out.append("零");
}
}
}
out.append(printRmb8(array, end - 8, end));
return out.toString();
}
/**
* 打印小数,只打印两位小数
* @param array
* @param start
* @param end
* @return
*/
private String printFlt(char []array, int start, int end) {
StringBuilder out = new StringBuilder();
char []rmb = {'角', '分'};
for (int i = start; i < start + 2 && i < end; i++) {
out.append(numStr[Integer.parseInt(array[i] +"")]);
out.append(rmb[i-start]);
}
return out.toString();
}
public static void main(String args[]) {
System.out.println(new NumToRmb("0.1234").numToRmb());
}
* 将浮点数转换成人民币读法字符串
* @author joy
*
*/
public class NumToRmb {
private static char []numStr
= {'零', '壹','贰','叁','肆','伍','陆','柒','捌','玖','拾'};
private String num;
NumToRmb (String num) {
this.num = num;
}
/**
* 整数位只支持12位以内,即最大识别范围为千亿
* @return
*/
public String numToRmb() {
//去掉分隔的逗号
String tmp = num.replaceAll("\\,", "");
char []numArray = tmp.toCharArray();
int lenInt = 0;
String out = null;
lenInt = tmp.indexOf('.');
if (lenInt == -1) {
lenInt = tmp.length();
}
if (lenInt >= 13) {
System.out.println("此方法只能识别13位以内的数字,即最大支持范围为千亿");
return null;
}
if (lenInt <= 4) {
out = printRmb4(numArray, 0, lenInt);
}
if (lenInt <= 8 && lenInt > 4) {
out = printRmb8(numArray, 0, lenInt);
}
if (lenInt <= 12 && lenInt > 8) {
out = printRmb12(numArray, 0, lenInt);
}
out += printFlt(numArray, lenInt+1, numArray.length);
return out;
}
private String printRmb4(char []array, int start, int end) {
StringBuilder out = new StringBuilder();
int len = end - start;
char []rmb = {'千', '百', '十', '元'};
for (int i = start; i < end; i++) {
if (array[i] != '0') {
out.append(numStr[Integer.parseInt(array[i] +"")]);
out.append(rmb[4-len+i-start]);
} else {
if (i > start && array[i-1] != '0') {
out.append("零");
}
}
}
return out.toString();
}
private String printRmb8(char []array, int start, int end) {
StringBuilder out = new StringBuilder();
int len = end - start - 4;
char []rmb = {'千', '百', '十', '万'};
for (int i = start; i < end - 4; i++) {
if (array[i] != '0') {
out.append(numStr[Integer.parseInt(array[i] +"")]);
out.append(rmb[4-len+i-start]);
} else {
if (i > start && array[i-1] != '0') {
out.append("零");
}
}
}
out.append(printRmb4(array, end - 4, end));
return out.toString();
}
private String printRmb12(char []array, int start, int end) {
StringBuilder out = new StringBuilder();
int len = end - start - 8;
char []rmb = {'千', '百', '十', '亿'};
for (int i = start; i < end - 8; i++) {
if (array[i] != '0') {
out.append(numStr[Integer.parseInt(array[i] +"")]);
out.append(rmb[4-len+i-start]);
} else {
if (i > start && array[i-1] != '0') {
out.append("零");
}
}
}
out.append(printRmb8(array, end - 8, end));
return out.toString();
}
/**
* 打印小数,只打印两位小数
* @param array
* @param start
* @param end
* @return
*/
private String printFlt(char []array, int start, int end) {
StringBuilder out = new StringBuilder();
char []rmb = {'角', '分'};
for (int i = start; i < start + 2 && i < end; i++) {
out.append(numStr[Integer.parseInt(array[i] +"")]);
out.append(rmb[i-start]);
}
return out.toString();
}
public static void main(String args[]) {
System.out.println(new NumToRmb("0.1234").numToRmb());
}
}