数字转成大写

1,本程序是一个关于数字转化成大写的小程序,支持小数,例如:123 一百二十三,56.78 五十六点七八
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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值