题解 | #人民币转换#
人民币转换
http://www.nowcoder.com/practice/00ffd656b9604d1998e966d555005a4b
public class Main {
private static String[] word = new String[]{"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
//小数点之前
private static String[] frontUnits = new String[]{"元", "拾", "佰", "仟", "万", "亿", "整"};
//小数点之后
private static String[] behindUnits = new String[]{"角", "分"};
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
System.out.println(getMoney(scanner.nextLine()));
}
scanner.close();
}
private static String getMoney(String money) {
int indexOfDot = money.indexOf(".");
//如果没有点,则补充点。
if (indexOfDot < 0) {
money = money + ".00";
} else if (indexOfDot == money.length() - 2) {
money = money + "0";
}
indexOfDot = money.indexOf(".");
StringBuilder frontBuilder = new StringBuilder(money.substring(0, indexOfDot));
StringBuilder behindBuilder = new StringBuilder(money.substring(indexOfDot + 1));
return getFrontMoney(frontBuilder) + (getBehindMoney(behindBuilder));
}
/**
* 获取点之前的money
* 1仟1佰1拾1亿 1仟1佰1拾1万 1仟1佰1拾1元
*
* @param frontBuilder
* @return
*/
private static String getFrontMoney(StringBuilder frontBuilder) {
StringBuilder frontSb = new StringBuilder();
frontSb.append("人民币");
for (int i = 0; i < frontBuilder.length(); i++) {
char currentChar = frontBuilder.charAt(i);
int currentNum = Integer.parseInt(currentChar + "");
int frontIndex = frontBuilder.length() - i - 1;
switch (currentNum) {
case 0:
if (i > 0) {
char lastChar = frontBuilder.charAt(i - 1);
int lastNum = Integer.parseInt(lastChar + "");
System.out.println("lastNum:" + lastNum);
if (lastNum != 0 && frontIndex % 4 != 0 && frontIndex % 4 != 1) {
frontSb.append(word[currentNum]);
}
}
break;
case 1:
if (frontIndex != 1) {
frontSb.append(word[currentNum]);
}
break;
default:
frontSb.append(word[currentNum]);
break;
}
//倒序的获取单位("元", "拾", "佰", "仟", "万", "亿", "整")
if (currentNum != 0) {
frontSb.append(getFrontUnit(frontIndex));
} else {
if (frontIndex == 0 && i != 0) {
frontSb.append(getFrontUnit(frontIndex));
}
}
}
return frontSb.toString();
}
/**
* 获取点之后的money
* . 1角1分
*
* @param behindBuilder
* @return
*/
private static String getBehindMoney(StringBuilder behindBuilder) {
StringBuilder behindSb = new StringBuilder();
for (int i = 0; i < behindBuilder.length(); i++) {
char currentChar = behindBuilder.charAt(i);
int currentNum = Integer.parseInt(currentChar + "");
if (currentNum == 0) {
if (i == 0) {
int nextNum = Integer.parseInt(behindBuilder.charAt(i + 1) + "");
if (nextNum == 0) {
behindSb.append(frontUnits[6]);
}
}
} else {
behindSb.append(word[currentNum]);
behindSb.append(getBehindUnit(i));
}
}
return behindSb.toString();
}
/**
* 获取点之前的单位
* 1仟1佰1拾1亿 1仟1佰1拾1万 1仟1佰1拾1元 . 1角1分
*
* @param index 点之前各数的index:靠近小数点的为圆点
* @return 小数点之前的单位
*/
private static String getFrontUnit(int index) {
int x = index % 4;
int i = index / 4;
String unit = "";
switch (x) {
case 3://仟
unit = frontUnits[3];
break;
case 0://元,万,亿
switch (i) {
case 0://元
//2、中文大写金额数字到“元”为止的,在“元”之后,应写“整字,如532.00应写成“人民币伍佰叁拾贰元整”。在”角“和”分“后面不写”整字。
unit = frontUnits[0];
break;
case 1://"万"
unit = frontUnits[4];
break;
case 2://"亿"
unit = frontUnits[5];
break;
default:
break;
}
break;
case 2://佰
unit = frontUnits[2];
break;
case 1://拾
unit = frontUnits[1];
break;
default:
break;
}
return unit;
}
/**
* 获取点之后的单位
* . 1角1分
*
* @param index 点之后各数的index 靠近小数点的为圆点
* @return 小数点之后的单位
*/
private static String getBehindUnit(int index) {
return behindUnits[index];
}
}