本帖最后由 武剑峰 于 2012-5-31 16:45 编辑
又改了一下,不过不能计算兆以上数字,注释没有写全。总体思想是将输入作为字符串处理,判断输入合法;然后当有小数部分时,将小数点前后的数据分成两块处理:整数部分存储到long型中,小数部分放到int型中(小数部分自动截取为两位,多则舍去,少则末尾补零)。如果没有小数直接转为long处理整数部分即可。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**@athor wujianfeng
* @application 将输入的数字转换成汉字大写金额
*/
public class UperMoney {
private static final char[] uperHanzi = new char[] { '零', '壹', '贰', '叁',
'肆', '伍', '陆', '柒', '捌', '玖' };
private static final char[] units = new char[] { '分', '角', '元', '拾', '佰',
'仟', '万', '拾', '佰', '仟', '亿', '拾', '佰', '仟', '万' };
public UperMoney(){}
public static void main(String[] args) {
String readString = "";
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
System.out.print("请输入要显示大写的数值:");
try {
readString = br.readLine();// 将输入作为字符串保存
while (!readString.equals("")) {
System.out.println("数值" + readString + " 的大写为:"
+ new UperMoney().UperMoney(readString));
System.out.print("请输入要显示大写的数值:");
readString = br.readLine();
}
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String UperMoney(String money) {
char[] ch = money.toCharArray();// 将输入的字符串转换为字符数组
boolean isDCode = false; // 判断输入是否合法标志
for (int i = 0; i < ch.length; i++) {//判断数字是否是有1234567890.组成
switch (ch) {
case '0':
isDCode = true;
break;
case '1':
isDCode = true;
break;
case '2':
isDCode = true;
break;
case '3':
isDCode = true;
break;
case '4':
isDCode = true;
break;
case '5':
isDCode = true;
break;
case '6':
isDCode = true;
break;
case '7':
isDCode = true;
break;
case '8':
isDCode = true;
break;
case '9':
isDCode = true;
break;
case '.':
isDCode = true;
break;
default:
isDCode = false;
}
if (!isDCode) {
return "您输入不合法,可能包含非数字或负数";
}
}
StringBuffer sb = new StringBuffer();
String moneyz = null, moneyx = "",moneyhz="";
if (money.contains(".")) { // 假如输入时包含了小数点“.”
moneyz = money.substring(0, money.indexOf('.'));
moneyx = money.substring(money.indexOf('.') + 1);
if (moneyx.length() > 2) {
moneyx = moneyx.substring(0, 2); // 小数只能精确到两位,多余时自动舍去
}else if(moneyx.length() ==1){
moneyhz="整";
moneyx=moneyx+"0";
}
} else {
moneyz = money;
}
if(moneyz.length()>13){
return "数据超出范围,本程序不能计算兆及以上数字";
}
long intshu = 0, xsshu = 0;
if (!moneyz.isEmpty()) {
intshu = Long.valueOf(moneyz);// 整数部分
}
if (!moneyx.isEmpty()) {
xsshu = Long.valueOf(moneyx);// 小数部分
}else{
moneyhz="整";
}
boolean writeZero = false, iswan = true; // 是否写零
if (intshu / 10000 % 10000 == 0) {// 判断亿~万之间是不是全是“0”
iswan = false;
}
int unit = 0;
while (xsshu != 0) { // 写小数部分大写
int number = (int) xsshu % 10;
if (number > 0) {
sb.insert(0, units[unit]);
sb.insert(0, uperHanzi[number]);
writeZero = true;
} else if (writeZero) {
sb.insert(0, uperHanzi[number]);
writeZero = false; // 是否写零
}
unit++;
xsshu /= 10;
}
unit = 2; //将单位指针移到元
writeZero = false; // 是否写零
while (intshu != 0) { // 添加整数部分的大写
int number = (int) intshu % 10;
if (number > 0) {
sb.insert(0, units[unit]);
sb.insert(0, uperHanzi[number]);
writeZero = true;
} else if (writeZero) {
sb.insert(0, uperHanzi[number]);
writeZero = false; // 是否写零
}
if (unit == 2 && writeZero == false) {
sb.insert(0, units[unit]);
} else if (unit == 6 && (!writeZero && iswan)) {
sb.insert(0, units[unit]);
} else if (unit == 10 && !writeZero) {
sb.insert(0, units[unit]);
}
unit++;
intshu /= 10;
}
return sb.toString()+moneyhz;
}
}
|