|
I also join in the fun:)
-----------------------------------------
public final class RMB{
private RMB(){
}
/**
* Convert the given Arabic numerals into Chinese capital money
*
* @param input given Arabic numerals
* @return Chinese capitalized money
*/
public static String toRMB(String input) {
if (input == null || input.equals("")) {
return "zero";
}
// Determine whether the input is a number (the method is not posted, first comment)
//if (!isNumber(input)) {
// throw new IllegalArgumentException(
// "The money input must be a number!");
//}
// Determine whether there is a decimal point (round up the given string. The method is not posted, first comment)
//input = format(round(toDouble(input).doubleValue()));
int dotIndex = input.indexOf(".");
if (dotIndex> 0) {
if (dotIndex == input.length()-2) {
input = input + "0";
}
else if (dotIndex == input.length()-1) {
input = input + "00";
}
}
else {
input = input + ".00";
}
// Maximum number of digits: 15 digits
if (dotIndex> 14) {
throw new IllegalArgumentException(
"The money input is too large to convert!");
}
final String[] numbers = new String[] {"zero", "one", "two", "three", "4", "wu",
"Lu", "柒", "捌", "玖"};
final String[] units = new String[] {"分", "角", "元", "take", "百", "千", "万",
"Pick up", "Hundred", "Thousand", "One Hundred", "One Hundred", "One Hundred", "Thousand", "Million"};
final String[] invalids = new String[] {"Zero Yuan", "Zero[Thousand Hundred Ten Yuan Jiao Min]", "Zero+", "Zero Billion",
"Zero million", "Zero Yuan", "100 million*+10,000"};
final String[] valids = new String[] {"Pick up yuan", "zero", "zero", "billion", "million", "yuan",
"Billion"};
// Reverse the given number
StringBuffer inputReversed = new StringBuffer(input).reverse();
inputReversed.deleteCharAt(inputReversed.indexOf("."));
// Convert by bit and add units
StringBuffer resultBuffer = new StringBuffer();
int numberLength = inputReversed.length();
for (int index = 0; index <numberLength; index++) {
resultBuffer.append(units[index]);
resultBuffer.append(numbers[Integer.parseInt(inputReversed.charAt(index)
+ "")]);
}
// Replace illegal expression
String result = resultBuffer.reverse().toString();
for (int i = 0; i <invalids.length; i++) {
result = result.replaceAll(invalids[i], valids[i]);
}
// If it starts with a zero, remove the zero
if (result.startsWith("zero")) {
result = result.substring(1, result.length());
}
// If it ends with a zero, remove the zero
if (result.endsWith("zero")) {
result = result.substring(0, result.length()-1);
}
// If there is no corner, add the whole word
if (result.indexOf("angle") <0&&result.indexOf("分") <0) {
result = result + "full";
}
return result;
}
public static void main(String[] args) {
String money = "100000000010.12645";
System.out.println(RMB.toRMB(money));
String moneyString = "10.01";
System.out.println(RMB.toRMB(moneyString));
}
} |
|