重新写了一个Java中文大写金额转换。
中文大写金额单位没有 '兆','厘' 。 可以修改UNIT 和DECIMAL 数组.
/**
* @author QWang
*
*/
public class ChineseMoney {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
String s = "1234567890.500";
System.out.println(chineseMoney(s));
}
private static final String[] UNIT = {"元","万","亿","兆"};
//private static final String[] UNIT = {"元","万","亿"};
private static final String[] BIT = {"","拾","佰","仟"};
private static final String[] INT = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
private static final String[] DECIMAL = {"","角","分","厘"};
//private static final String[] DECIMAL = {"","角","分"};
//private static final String[] DECIMAL = {"","角","分","厘","毫"};
private static final String FULL = "整";
public static String chineseMoney(int number){
return chineseMoney(String.valueOf(number));
}
public static String chineseMoney(float number){
return chineseMoney(String.valueOf(number));
}
public static String chineseMoney(double number){
return chineseMoney(String.valueOf(number));
}
public static String chineseMoney(String number){
number = number.replaceAll(",", "");
double d = Double.parseDouble(number);
/
/// INTEGER ///
/
StringBuffer sb = new StringBuffer();
char[] integer = String.valueOf((long)d).toCharArray();
boolean segno = false;
boolean lastKilo = false;
for (int iunit=0, i=integer.length-1; i>=0; iunit++) {
String unit = UNIT[iunit];
StringBuffer temp = new StringBuffer();
for (int j=0; j<4 && i>=0; j++,i--) {
int n = Character.getNumericValue(integer[i]); //number
String bit = BIT[j]; //位
String num = INT[n]; //中文数字
if (n>0) {
temp.insert(0, num + bit);
segno = false;
if (j==3)
lastKilo = true;
}
else {
if (j!=0) {
if (!segno) {
temp.insert(0, num);
}
}
else if (lastKilo)
sb.insert(0,INT[0]);
segno = true;
if (j==3)
lastKilo = false;
}
}
if (!Util.isEmptyString(temp) || (iunit==0 && (long)d>0) )
sb.insert(0, unit); //中文单位
sb.insert(0, temp); //
}
/
/// DECIMAL ///
/
StringBuffer sb2 = new StringBuffer();
char[] decimal = String.valueOf(
Util.stringReplace(Util.getFormatDecimal(d%10, "0.###"), ".", "")).toCharArray();
boolean lastZero = false;
for (int dc=0; dc<decimal.length; dc++) {
int n = Character.getNumericValue(decimal[dc]);
if (n>0) {
if (dc!=0) {
sb2.append(INT[n]);
sb2.append(DECIMAL[dc]);
}
lastZero = false;
}
else {
if (!lastZero && (long)d>0 && d%1>0)
sb2.append( INT[0]);
lastZero = true;
}
}
if (d!=0 && decimal.length<=2)
sb2.append(FULL);
sb.append(sb2); //append decimal end.
return sb.toString();
}
public static boolean isEmptyString(Object s) {
return (s==null || s.toString().trim().length()==0 || s.toString().trim().equalsIgnoreCase("null"));
}
public static String getFormatDecimal(double value, String pattern) {
return (new DecimalFormat(pattern)).format((double)value);
}
}
中文大写金额单位没有 '兆','厘' 。 可以修改UNIT 和DECIMAL 数组.
/**
* @author QWang
*
*/
public class ChineseMoney {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
String s = "1234567890.500";
System.out.println(chineseMoney(s));
}
private static final String[] UNIT = {"元","万","亿","兆"};
//private static final String[] UNIT = {"元","万","亿"};
private static final String[] BIT = {"","拾","佰","仟"};
private static final String[] INT = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
private static final String[] DECIMAL = {"","角","分","厘"};
//private static final String[] DECIMAL = {"","角","分"};
//private static final String[] DECIMAL = {"","角","分","厘","毫"};
private static final String FULL = "整";
public static String chineseMoney(int number){
return chineseMoney(String.valueOf(number));
}
public static String chineseMoney(float number){
return chineseMoney(String.valueOf(number));
}
public static String chineseMoney(double number){
return chineseMoney(String.valueOf(number));
}
public static String chineseMoney(String number){
number = number.replaceAll(",", "");
double d = Double.parseDouble(number);
/
/// INTEGER ///
/
StringBuffer sb = new StringBuffer();
char[] integer = String.valueOf((long)d).toCharArray();
boolean segno = false;
boolean lastKilo = false;
for (int iunit=0, i=integer.length-1; i>=0; iunit++) {
String unit = UNIT[iunit];
StringBuffer temp = new StringBuffer();
for (int j=0; j<4 && i>=0; j++,i--) {
int n = Character.getNumericValue(integer[i]); //number
String bit = BIT[j]; //位
String num = INT[n]; //中文数字
if (n>0) {
temp.insert(0, num + bit);
segno = false;
if (j==3)
lastKilo = true;
}
else {
if (j!=0) {
if (!segno) {
temp.insert(0, num);
}
}
else if (lastKilo)
sb.insert(0,INT[0]);
segno = true;
if (j==3)
lastKilo = false;
}
}
if (!Util.isEmptyString(temp) || (iunit==0 && (long)d>0) )
sb.insert(0, unit); //中文单位
sb.insert(0, temp); //
}
/
/// DECIMAL ///
/
StringBuffer sb2 = new StringBuffer();
char[] decimal = String.valueOf(
Util.stringReplace(Util.getFormatDecimal(d%10, "0.###"), ".", "")).toCharArray();
boolean lastZero = false;
for (int dc=0; dc<decimal.length; dc++) {
int n = Character.getNumericValue(decimal[dc]);
if (n>0) {
if (dc!=0) {
sb2.append(INT[n]);
sb2.append(DECIMAL[dc]);
}
lastZero = false;
}
else {
if (!lastZero && (long)d>0 && d%1>0)
sb2.append( INT[0]);
lastZero = true;
}
}
if (d!=0 && decimal.length<=2)
sb2.append(FULL);
sb.append(sb2); //append decimal end.
return sb.toString();
}
public static boolean isEmptyString(Object s) {
return (s==null || s.toString().trim().length()==0 || s.toString().trim().equalsIgnoreCase("null"));
}
public static String getFormatDecimal(double value, String pattern) {
return (new DecimalFormat(pattern)).format((double)value);
}
}