阿拉伯数字大写转换
转载请注明出处,欢迎讨论!
public class GenChinese {
private final static String[] STR_CHINESE_NUMBER={
"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"
};
private final static String[] STR_SHU_JI = {
"亿","万",""
}; //大数量级
private final static String[] STR_SHU_JI_2={
"千","佰","拾",""
};//千内数量级
public GenChinese() {
}
/**
*
* @param intNumber 需要转换的整数
* @return 返回汉字大写数字
*/
public String genChinese(int intNumber){
StringBuffer rets = new StringBuffer(); //返回字符串
int ret; //临时变量
ret = intNumber;
int i=1; //存储数字长度
while((ret=ret/10)!=0){
i++; //计算数字长度
}
//构建一维数组
int[] intarrrayA=new int[i];
ret = intNumber;
int m = i; //m临时变量
for(m=m-1;m>=0;m--){
intarrrayA[m]=ret%10; //取余
//System.out.println(intarrrayA[m]);
ret = ret/10;
}
//构建二维数组
int rows=1; //二维数组的行数变量
if(i%4==0){
rows=i/4;
}else{
rows=i/4+1;
}
m = i-1; //存储数组下标临时变量
int[][] intArrayB = new int[rows][4];
for(int n=rows-1;n>=0;n--){
for(int a=3;a>=0;a--){
if(m>=0){
intArrayB[n][a] = intarrrayA[m];
}else{
intArrayB[n][a] = 0; //不足4列的前补零,形如数字44构建二维数组维{{0,0,4,4}}
}
m--;
}
}
//打印intArrayB
/**
for(int a=0;a for(int b=0;b<4;b++){
if(b==3){
System.out.println(intArrayB[a][b]);
}else{
System.out.print(intArrayB[a][b]);
}
}
}
*/
//打印汉字过程
int iFlag = 0; //第一行的前面0数字的处理标志
int iZero = 0;
for(int a=0;a for (int b = 0; b < 4; b++) {
if(a==0&&intArrayB[a][b]==0&&iFlag==0){
;
}else{
iFlag = 1; //
if(a==0){ //第一行处理状况,整形不会超过3行
if(iZero==0){ //没有出现零
if(intArrayB[a][b]!=0){
rets.append(STR_CHINESE_NUMBER[intArrayB[a][b]]);
rets.append(STR_SHU_JI_2[b]); //加小数量级
}else{
iZero++;
}
}else{
if(intArrayB[a][b]!=0){
rets.append(STR_CHINESE_NUMBER[0]).append(STR_CHINESE_NUMBER[intArrayB[a][b]]);
rets.append(STR_SHU_JI_2[b]); //加小数量级
iZero = 0;
}else{
if(b==3){
rets.append(STR_CHINESE_NUMBER[0]);
iZero = 0;
}else{
iZero++;
}
}
}
}else if(a>0){
if (iZero == 0) {
if (intArrayB[a][b] != 0) {
rets.append(STR_CHINESE_NUMBER[intArrayB[a][b]]);
rets.append(STR_SHU_JI_2[b]); //加小数量级
}
else {
iZero++;
}
}
else {
if (intArrayB[a][b] != 0) {
rets.append(STR_CHINESE_NUMBER[0]).append(STR_CHINESE_NUMBER[
intArrayB[a][b]]);
rets.append(STR_SHU_JI_2[b]); //加小数量级
iZero = 0;
}
else {
if(b==3&&a!=rows-1){
rets.append(STR_CHINESE_NUMBER[0]);
iZero = 0;
}else{
iZero++;
}
}
}
}
}
}
if(rows==3) //判断大数量级别读取
rets.append(STR_SHU_JI[a]);
else if(rows==2)
rets.append(STR_SHU_JI[a+1]);
else if(rows==1)
rets.append("");
}
return rets.toString();
}
static void printArray(int[] arrayInt){
for(int i=0;i if(i==arrayInt.length-1)
System.out.println(arrayInt[i]);
else
System.out.print(arrayInt[i]+",");
}
public static void main(String[] args){
GenChinese g = new GenChinese();
long m = System.currentTimeMillis();
System.out.println(g.genChinese(1000050404));
long n = System.currentTimeMillis();
System.out.println("运行了"+(n-m));
}
}
上一篇: httpClient应用