1,本程序是一个关于数字转化成大写的小程序,支持小数,例如:123 一百二十三,56.78 五十六点七八
2,目前支持的最大位数到兆为止,请看程序
2,目前支持的最大位数到兆为止,请看程序
/**
*
*/
package com.img.collection;
import java.util.Scanner;
import java.util.regex.Pattern;
/**
* change the number from lower to upper(e.g.1:壹)
*
* @author Bruce
* @date Sep 24, 2011
* @version
*/
public class UpperCaseCharacter {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = null;
while (true) {
System.out.println("Type number:");
str = sc.next();
if (!str.matches("\\d+\\.?\\d*")) {
System.out.println("Type format is invalid");
continue;
}
String[] numInfo = str.split("\\.");
if (!str.matches("\\d+\\.\\d*")) {
System.out.println(parser(numInfo[0]));
} else {
String before = numInfo[0];
String tail = numInfo[1];
System.out.println(parser(before, tail));
}
break;
}
}
/**
* TODO
*
* @param before
* @param tail
*/
private static String parser(String before, String tail) {
String start = parser(before);
int length = tail.length();
StringBuilder sb = new StringBuilder();
String end = null;
for (int i = 0; i < length; i++) {
char ch = tail.charAt(i);
sb.append(convertNum(ch));
}
end = sb.toString();
return start + "点" + end;
}
private static String convertNum(char c) {
switch (c) {
case '0':
return "零";
case '1':
return "壹";
case '2':
return "贰";
case '3':
return "叁";
case '4':
return "肆";
case '5':
return "五";
case '6':
return "六";
case '7':
return "七";
case '8':
return "八";
case '9':
return "玖";
default:
break;
}
return null;
}
private static String convert(int c) {
switch (c) {// 亿,千(万),百(万), 十(万), 万,千,百,十, 897654
case 1:
return "十";
case 2:
return "百";
case 3:
return "千";
case 4:
return "万";
case 5:
return "十";
case 6:
return "百";
case 7:
return "千";
case 8:
return "亿";
case 9:
return "十";
case 10:
return "百";
case 11:
return "千";
case 12:
return "万";
case 13:
return "兆";
default:
break;
}
return null;
}
/**
* TODO
*
* @param str
* @return
*/
private static String parser(String str) {
int length = str.length();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i++) {
char ch = str.charAt(i);
sb.append(convertNum(ch));
if (length - 1 - i != 0) {
sb.append(convert(length - 1 - i));
}
}
return sb.toString();
}// 909878.
}// 亿,千(万),百(万), 十(万), 万,千,百,十, 897654