Topic: 关于货币大小写转换的方法?

  Print this page

1.关于货币大小写转换的方法? Copy to clipboard
Posted by: candle999
Posted on: 2003-05-22 10:24

有誰知道货币大小写转换的方法怎么写吗?
例如 3000 转换后成 叁仟圆正

2.Re:关于货币大小写转换的方法?转载 [Re: candle999] Copy to clipboard
Posted by: lwwbill
Posted on: 2003-05-22 17:01

/**
* description 根据人民币的数字转化成中文的大写形式
* @param String type 人民币数字
* @return String 大写形式的字符串
* @author
* date: 2002-10-20
*/
public static String getChineseMoney(String moneyNum){
//存放汉字的数组
String[] chinese = new String[17];
String[] money = new String[2];
chinese[0]="零";
chinese[1]="壹";
chinese[2]="贰";
chinese[3]="叁";
chinese[4]="肆";
chinese[5]="伍";
chinese[6]="陆";
chinese[7]="柒";
chinese[8]="捌";
chinese[9]="玖";
chinese[10]="拾";
chinese[11]="佰";
chinese[12]="仟";
chinese[13]="万";
chinese[14]="亿";
chinese[15]="元" ;
chinese[16]="整";
money[0]="角";
money[1]="分";
//输出的大写形式字符串
String str_out="";
//整数部分字符串
String str_left="";
//小数部分字符串
String str_right="";
//小数点
String str_point=".";
//小数点位置
int ponitLocation=moneyNum.indexOf(".");
//小数点后位数
int pointLater=moneyNum.length() - ponitLocation - 1;
//分离字符串为两部分
if(pointLater>1)
{
str_left=moneyNum.substring(0,ponitLocation);
str_right=moneyNum.substring(ponitLocation+1,ponitLocation+3);
}
//整数部分字符串的长度
int str_left_length;
str_left_length=str_left.length() ;
//小数部分字符串的长度
int str_right_length;
str_right_length=str_right.length();
//整数部分的零字符标识位
int flag=0;
//小数部分的零字符标识位
int flag_zreo=0;
//开始转换整数部分
for(int i=0;i<=str_left.length()-1;i++)
{

String str_l;
str_l=str_left.substring(i,i+1);
int temp = Integer.parseInt(str_l);
switch(temp)
{
case 1:
str_out=str_out+chinese[1];
break;
case 2:
str_out=str_out+chinese[2];
break;
case 3:
str_out=str_out+chinese[3];
break;
case 4:
str_out=str_out+chinese[4];
break;
case 5:
str_out=str_out+chinese[5];
break;
case 6:
str_out=str_out+chinese[6];
break;
case 7:
str_out=str_out+chinese[7];
break;
case 8:
str_out=str_out+chinese[8];
break;
case 9:
str_out=str_out+chinese[9];
break;
}
//对整数部分的零的处理
if(temp==0)
{
flag++;
if(str_left_length==1)
{
str_out=str_out+chinese[15];
}
if(str_left_length==9)
{
flag=0;
str_out=str_out+chinese[14];
}
if(str_left_length==5)
{
flag=0;
str_out=str_out+chinese[13];
}
//判断是否输出零,根据它的下一位来决定:是零则不输出,否则输出
if(str_left_length>=2)
{
String str_le=str_left.substring
(i+1,i+2);
int tem=Integer.parseInt(str_le);
if((flag==1)&&(tem!=0))
str_out=str_out+chinese[0];
else
flag=0;
}
str_left_length--;
}
else
{
flag=0;
//添加整数部分的单位:拾, 佰,仟 ,万,亿
switch(str_left_length)
{

case 1:
str_out=str_out+chinese[15];
str_left_length--;
break;
case 2:
str_out=str_out+chinese[10];
str_left_length--;
break;
case 3:
str_out=str_out+chinese[11];
str_left_length--;
break;
case 4:
str_out=str_out+chinese[12];
str_left_length--;
break;
case 5:
str_out=str_out+chinese[13];
str_left_length--;
break;
case 6:
str_out=str_out+chinese[10];
str_left_length--;
break;
case 7:
str_out=str_out+chinese[11];
str_left_length--;
break;
case 8:
str_out=str_out+chinese[12];
str_left_length--;
break;
case 9:
str_out=str_out+chinese[14];
str_left_length--;
break;
case 10:
str_out=str_out+chinese[10];
str_left_length--;
break;
case 11:
str_out=str_out+chinese[11];
str_left_length--;
break;
case 12:
str_out=str_out+chinese[12];
str_left_length--;
break;
case 13:
str_out=str_out+chinese[13];
str_left_length--;
break;
}

}
}
//处理小数部分的字符串
for(int i=0;i<=str_right.length()-1;i++)
{
String str_r;
str_r=str_right.substring(i,i+1);
int temp1 = Integer.parseInt(str_r);
switch(temp1)
{
case 1:
str_out=str_out+chinese[1];
break;
case 2:
str_out=str_out+chinese[2];
break;
case 3:
str_out=str_out+chinese[3];
break;
case 4:
str_out=str_out+chinese[4];
break;
case 5:
str_out=str_out+chinese[5];
break;
case 6:
str_out=str_out+chinese[6];
break;
case 7:
str_out=str_out+chinese[7];
break;
case 8:
str_out=str_out+chinese[8];
break;
case 9:
str_out=str_out+chinese[9];
break;

}
//对小数部分的零的处理
if(temp1==0)
{
flag_zreo++;
if(str_right_length==2)
{
//判断是否输出零,根据它的下一位来决定:是零则不输出,否则输出
String str_ri=str_right.substring(i+1,i+2);
int temp=Integer.parseInt(str_ri);
if((flag_zreo==1)&&(temp!=0))
{
str_out=str_out+chinese[0];
}
}
else
{
str_out=str_out+chinese[16];
}
str_right_length--;
}
else
{
//添加小数部分的角、分
switch(str_right_length)
{
case 1:
str_out=str_out+money[1];
str_right_length--;
break;
case 2:
str_out=str_out+money[0];
str_right_length--;
break;
}
}
}
return str_out;
}

3.Re:关于货币大小写转换的方法? [Re: candle999] Copy to clipboard
Posted by: candle999
Posted on: 2003-05-23 08:51

谢谢了!Smile

4.Re:关于货币大小写转换的方法? [Re: candle999] Copy to clipboard
Posted by: bbbaby
Posted on: 2003-06-02 18:03

受教了,谢谢

5.Re:关于货币大小写转换的方法? [Re: candle999] Copy to clipboard
Posted by: floater
Posted on: 2003-06-03 01:41

Man, this is cobol style, with a lot of switch and if statements.

Refactor it!


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923