package com.mytest;
import java.util.HashMap;
public class RMBCurrency {
HashMap chars;
StringBuffer ChnMoney;
public RMBCurrency() {
super();
init();
}
private void init() {
ChnMoney = new StringBuffer();
chars = new HashMap();
chars.put("+0", "整");
chars.put("-1", "角");
chars.put("-2", "分");
chars.put("0", "零");
chars.put("1", "壹");
chars.put("2", "贰");
chars.put("3", "叁");
chars.put("4", "肆");
chars.put("5", "伍");
chars.put("6", "陆");
chars.put("7", "柒");
chars.put("8", "捌");
chars.put("9", "玖");
chars.put("00", "拾");
chars.put("000", "佰");
chars.put("0000", "仟");
chars.put(new Integer(0), "元");
chars.put(new Integer(1), "万");
chars.put(new Integer(2), "亿");
chars.put(new Integer(3), "兆");
}
public String convertMoney(String amount) {
String ret = null;
try {
checkFormat(amount);
} catch (NumberFormatException e) {
System.out.println(e.getMessage());
return null;
}
if (amount.indexOf(".") > 1 || amount.charAt(0) != '0') {
// numbers before the DOT is divided into several parts
// four number as a part
// Note the 1st part naming head may have any number from 1 to 4
char[] head;
int parts = amount.indexOf(".") / 4;
if (amount.indexOf(".") - parts * 4 == 0) {
head = new char[4];
amount.getChars(0, 4, head, 0);
parts = parts - 1;
} else {
head = new char[amount.indexOf(".") - parts * 4];
amount.getChars(0, amount.indexOf(".") - parts * 4, head, 0);
}
// deal with head
doBeforeDot(head, true, '-');
if (parts != 0) {
ChnMoney.append(chars.get(new Integer(parts)));
}
// deal with other parts except head
String[] temp = new String[parts];
for (int i = 0; i < parts; i++) {
temp[i] = amount.substring(amount.length() - 3 - 4 * (i + 1),
amount.length() - 3 - 4 * i);
}
char[] part = new char[4];
for (int i = temp.length - 1; i >= 0; i--) {
temp[i].getChars(0, 4, part, 0);
boolean isAll0 = true;
for (int j = 0; j < 4; j++) {
if (part[j] != '0') {
isAll0 = false;
}
}
if (!isAll0) {
if (i == temp.length - 1) {
doBeforeDot(part, false, head[head.length - 1]);
} else {
char[] chrArray = temp[i + 1].toCharArray();
doBeforeDot(part, false, chrArray[chrArray.length - 1]);
}
if (i != 0) {
ChnMoney.append(chars.get(new Integer(i)));
}
}
}
// always append "元" here
ChnMoney.append(chars.get(new Integer(0)));
}
// deal with numbers after the DOT
char[] afterDot = new char[2];
amount.getChars(amount.length() - 2, amount.length(), afterDot, 0);
if (afterDot[0] == '0' && afterDot[1] == '0') {
ChnMoney.append(chars.get("+0"));
} else {
doAfterDot(afterDot, amount);
}
ret = ChnMoney.toString();
ChnMoney = new StringBuffer();
return ret;
}
/*
* deal with numbers before the DOT
*/
private void doBeforeDot(char[] beforeDot, boolean isHead, char previous) {
int i = 0;
Character num;
String key;
if (isHead) {
i++;
num = new Character(beforeDot[0]);
ChnMoney.append(chars.get(num.toString()));
key = getKey(beforeDot.length);
if (chars.get(key) != null) {
ChnMoney.append(chars.get(key));
}
}
for (; i < beforeDot.length; i++) {
boolean all0 = true;
for (int j = i; j < beforeDot.length; j++) {
if (beforeDot[j] != '0') {
all0 = false;
}
}
if (all0) {
return;
}
num = new Character(beforeDot[i]);
if (i == 0 && beforeDot[i] != '0') {
if (previous == '0') {
ChnMoney.append(chars.get("0"));
}
ChnMoney.append(chars.get(num.toString()));
}
if (i == 0 && beforeDot[i] == '0') {
ChnMoney.append(chars.get(num.toString()));
} else if (i != 0 && beforeDot[i - 1] != '0') {
ChnMoney.append(chars.get(num.toString()));
} else if (i != 0 && beforeDot[i - 1] == '0' && beforeDot[i] != '0') {
ChnMoney.append(chars.get(num.toString()));
}
if (beforeDot[i] != '0') {
key = getKey(beforeDot.length - i);
if (chars.get(key) != null) {
ChnMoney.append(chars.get(key));
}
}
}
}
/*
* deal with numbers after the DOT
*/
private void doAfterDot(char[] afterDot, String amount) {
Character num = new Character(afterDot[0]);
if (amount.indexOf(".") != 1 || amount.charAt(0) != '0') {
if (amount.charAt(amount.length() - 4) == '0' || afterDot[0] == '0') {
ChnMoney.append(chars.get("0"));
}
}
if (afterDot[0] != '0') {
ChnMoney.append(chars.get(num.toString()));
ChnMoney.append(chars.get("-1"));
}
if (afterDot[1] != '0') {
num = new Character(afterDot[1]);
ChnMoney.append(chars.get(num.toString()));
ChnMoney.append(chars.get("-2"));
}
}
/*
* Get keys of "拾","佰","仟"
*/
private String getKey(int keynum) {
String ret = null;
switch (keynum) {
case 2:
ret = "00";
break;
case 3:
ret = "000";
break;
case 4:
ret = "0000";
break;
default:
break;
}
return ret;
}
private void checkFormat(String in) throws NumberFormatException {
new Double(in).doubleValue();
if (in.indexOf(".") == 0) {
throw new NumberFormatException("MUST have numbers before DOT.");
}
if (in.indexOf(".") != 1 && in.charAt(0) == '0') {
throw new NumberFormatException("First Number CANNOT be zero.");
}
if (in.indexOf(".") == -1) {
throw new NumberFormatException("Missing DOT !");
} else if (in.indexOf(".", in.indexOf(".") + 1) > 1) {
throw new NumberFormatException("Only ONE DOT allowed !");
}
if (in.indexOf(".") + 3 != in.length()) {
throw new NumberFormatException(
"Ought to have two numbers after DOT");
}
if (in.indexOf(".") == 1 && in.charAt(2) == '0' && in.charAt(3) == '0') {
throw new NumberFormatException("CANNOT all be zero.");
}
}
public static void main(String[] args) {
RMBCurrency t = new RMBCurrency();
System.out.println(t.convertMoney("80505008800.05"));
System.out.println(t.convertMoney("369280500008801.10"));
System.out.println(t.convertMoney("10.00"));
}
}
>>>>>>>>>>>>>>>>>>>>>>>>>>> Refactoring 20061012 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
package com.mytest;
import java.util.HashMap;
public class RMBCurrency {
private HashMap chars;
private StringBuffer ChnMoney;
public RMBCurrency() {
super();
init();
}
private void init() {
ChnMoney = new StringBuffer();
chars = new HashMap();
chars.put("+0", "整");
chars.put("-1", "角");
chars.put("-2", "分");
chars.put("0", "零");
chars.put("1", "壹");
chars.put("2", "?");
chars.put("3", "?");
chars.put("4", "肆");
chars.put("5", "伍");
chars.put("6", "?");
chars.put("7", "?");
chars.put("8", "捌");
chars.put("9", "玖");
chars.put("20", "拾");
chars.put("30", "佰");
chars.put("40", "仟");
chars.put(new Integer(0), "元");
chars.put(new Integer(1), "万");
chars.put(new Integer(2), "?");
chars.put(new Integer(3), "兆");
}
public String convertMoney(String amount) {
String ret = null;
try {
checkFormat(amount);
} catch (NumberFormatException e) {
System.out.println(e.getMessage());
return null;
}
if (amount.indexOf(".") > 1 || amount.charAt(0) != '0') {
// numbers before the DOT is divided into several parts
// four number as a part
// Note the 1st part naming head may have any number from 1 to 4
char[] head;
int parts = amount.indexOf(".") / 4;
if (amount.indexOf(".") - parts * 4 == 0) {
head = new char[4];
amount.getChars(0, 4, head, 0);
parts = parts - 1;
} else {
head = new char[amount.indexOf(".") - parts * 4];
amount.getChars(0, amount.indexOf(".") - parts * 4, head, 0);
}
// deal with head
doBeforeDot(head, true, '-');
if (parts != 0) {
ChnMoney.append(chars.get(new Integer(parts)));
}
// deal with other parts except head
String[] temp = new String[parts];
for (int i = 0; i < parts; i++) {
temp[i] = amount.substring(amount.length() - 3 - 4 * (i + 1),
amount.length() - 3 - 4 * i);
}
char[] part = new char[4];
for (int i = temp.length - 1; i >= 0; i--) {
temp[i].getChars(0, 4, part, 0);
boolean isAll0 = true;
for (int j = 0; j < 4; j++) {
if (part[j] != '0') {
isAll0 = false;
}
}
if (!isAll0) {
if (i == temp.length - 1) {
doBeforeDot(part, false, head[head.length - 1]);
} else {
char[] chrArray = temp[i + 1].toCharArray();
doBeforeDot(part, false, chrArray[chrArray.length - 1]);
}
if (i != 0) {
ChnMoney.append(chars.get(new Integer(i)));
}
}
}
// always append "元" here
ChnMoney.append(chars.get(new Integer(0)));
}
// deal with numbers after the DOT
char[] afterDot = new char[2];
amount.getChars(amount.length() - 2, amount.length(), afterDot, 0);
if (afterDot[0] == '0' && afterDot[1] == '0') {
ChnMoney.append(chars.get("+0"));
} else {
doAfterDot(afterDot, amount);
}
ret = ChnMoney.toString();
ChnMoney = new StringBuffer();
return ret;
}
/*
* deal with numbers before the DOT
*/
private void doBeforeDot(char[] beforeDot, boolean isHead, char previous) {
int i = 0;
Character num;
if (isHead) {
i++;
num = new Character(beforeDot[0]);
ChnMoney.append(chars.get(num.toString()));
if (chars.get(beforeDot.length + "0") != null) {
ChnMoney.append(chars.get(beforeDot.length + "0"));
}
}
for (; i < beforeDot.length; i++) {
boolean all0 = true;
for (int j = i; j < beforeDot.length; j++) {
if (beforeDot[j] != '0') {
all0 = false;
}
}
if (all0) {
return;
}
num = new Character(beforeDot[i]);
if (i == 0 && beforeDot[i] != '0') {
if (previous == '0') {
ChnMoney.append(chars.get("0"));
}
ChnMoney.append(chars.get(num.toString()));
}
if (i == 0 && beforeDot[i] == '0') {
ChnMoney.append(chars.get(num.toString()));
} else if (i != 0 && beforeDot[i - 1] != '0') {
ChnMoney.append(chars.get(num.toString()));
} else if (i != 0 && beforeDot[i - 1] == '0' && beforeDot[i] != '0') {
ChnMoney.append(chars.get(num.toString()));
}
if (beforeDot[i] != '0') {
if (chars.get((beforeDot.length - i) + "0") != null) {
ChnMoney.append(chars.get((beforeDot.length - i) + "0"));
}
}
}
}
/*
* deal with numbers after the DOT
*/
private void doAfterDot(char[] afterDot, String amount) {
Character num = new Character(afterDot[0]);
if (amount.indexOf(".") != 1 || amount.charAt(0) != '0') {
if (amount.charAt(amount.length() - 4) == '0' || afterDot[0] == '0') {
ChnMoney.append(chars.get("0"));
}
}
if (afterDot[0] != '0') {
ChnMoney.append(chars.get(num.toString()));
ChnMoney.append(chars.get("-1"));
}
if (afterDot[1] != '0') {
num = new Character(afterDot[1]);
ChnMoney.append(chars.get(num.toString()));
ChnMoney.append(chars.get("-2"));
}
}
private void checkFormat(String in) throws NumberFormatException {
new Double(in).doubleValue();
if (in.indexOf(".") == 0) {
throw new NumberFormatException("MUST have numbers before DOT.");
}
if (in.indexOf(".") != 1 && in.charAt(0) == '0') {
throw new NumberFormatException(
"First Number CANNOT be zero when the amout > 9.");
}
if (in.indexOf(".") == -1) {
throw new NumberFormatException("Missing DOT !");
} else if (in.indexOf(".", in.indexOf(".") + 1) > 1) {
throw new NumberFormatException("Only ONE DOT allowed !");
}
if (in.indexOf(".") + 3 != in.length()) {
throw new NumberFormatException(
"Ought to have two numbers after DOT");
}
if (in.indexOf(".") == 1 && in.charAt(2) == '0' && in.charAt(3) == '0') {
throw new NumberFormatException("CANNOT all be zero.");
}
}
public static void main(String[] args) {
RMBCurrency t = new RMBCurrency();
System.out.println(t.convertMoney("80505008800.05"));
System.out.println(t.convertMoney("189580500008801.10"));
System.out.println(t.convertMoney("10.00"));
System.out.println(t.convertMoney("0.10"));
}
}