ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 数字转换类

数字转换类

原创 Linux操作系统 作者:dinner1007 时间:2019-02-20 19:30:05 0 删除 编辑
数字转换类
import java.io.*;

class ConvertNum
{
boolean bool = false;
int group;
int length;
int left;
String convertResult;

public void setData(String s)
{
length = s.length();
for(int i = 0 ;i {
if(!bool && s.charAt(i) == '.')
{
left = i;
if(i>12)
return;
bool = true;
if(i%4>0)
group = i/4+1;
else
group = i/4;
}
}
if(!bool)
{
left = length;
if(length%4>0)
group = (length)/4+1;
else
group = (length)/4;
}
splitStr(s);
}
public void splitStr(String s)
{
String leftStr[] = new String[group];
String right = "";
String temp = "";
for(int i = 1;i <= group;i++)
{
if(left<4)
{
leftStr[0] = s.substring(0,left);
}
else
{
if(i leftStr[group-i] = s.substring(left-4*i,left-4*(i-1));
else
leftStr[group-i] = s.substring(0,left-4*(i-1));
}
}

if(bool)
{
if(length-left<=3)
{
right = s.substring(left+1,length).trim();

if(right.length() < 1)
{
bool = false;
System.out.println("你输入的数据按整数计算!");
}
if(right.length() == 1)
temp = convertChar(right.charAt(0)) + "角零分";
if(right.length() == 2)
temp = convertChar(right.charAt(0)) + "角" + convertChar(right.charAt(1)) + "分";
}
else
{
right = s.substring(left+1,left+4).trim();
temp = convertChar(right.charAt(0)) + "角" + convertChar(right.charAt(1)) + "分";
}

//整数
if(!bool)
{
convertResult = convert(leftStr) + "整";
System.out.println(convertResult);
}
//小数
else
{
System.out.println("本系统保留两位小数,你输入的是: ");
convertResult = convert(leftStr) + temp;
System.out.println(convertResult);
}
}

public String convert(String s[])
{
String returnStr = "";
for(int i = 0;i< group;i++)
{
if((group-i) == 3)
{
returnStr = returnStr + convertSub(s[i]) + "亿";
System.out.println("亿");
}
if((group-i) == 2)
{
if(convertSub(s[i]).equals("")){}
else returnStr = returnStr + convertSub(s[i]) + "万";
System.out.println("万");
}
if((group-i) == 1)
{
returnStr = returnStr + convertSub(s[i]) + "元";
System.out.println("元");
}
}
return returnStr;
}

public String convertSub(String s)
{
String returnStr = "";
String tempStr[] = new String[s.length()];
int bl = s.length();
if(Integer.valueOf(s).intValue()%10 == 0)
bl = s.length()-1;

for(int j = 0;j {
if(group ==1 && left == 1 && s.charAt(0) == '0')
return "零";
int k = 4-s.length()+j;
switch(k)
{
case 0:
if(s.charAt(j) == '0') tempStr[j] = convertChar(s.charAt(j));
else tempStr[j] = convertChar(s.charAt(j)) + "仟";
break;
case 1:
if(s.charAt(j) == '0') tempStr[j] = convertChar(s.charAt(j));
else tempStr[j] = convertChar(s.charAt(j)) + "佰";
break;
case 2:
if(s.charAt(j) == '0') tempStr[j] = convertChar(s.charAt(j));
else tempStr[j] = convertChar(s.charAt(j)) + "拾";
break;
case 3:
if(s.charAt(j) == '0')
break;
tempStr[j] = convertChar(s.charAt(j));
break;
}
}

for(int j = 0;j {
if(s.length() == 2 && Integer.valueOf(s).intValue()%10 == 0)
return tempStr[0];
if((s.charAt(j) =='0' && s.charAt(j+1) =='0'))
{
j++;
if(Integer.valueOf(s).intValue()%1000 == 0)
return returnStr;
if(Integer.valueOf(s).intValue()%100 == 0)
return returnStr;
if(s.charAt(j+1) =='0')
{
j++;
returnStr = returnStr + tempStr[j].toString();
}
else returnStr = returnStr + tempStr[j].toString();
}
else returnStr = returnStr + tempStr[j].toString();
}
return returnStr;
}

public String convertChar(char i)
{
String s="";

switch(i)
{
case '0':
s="零";
break;
case '1':
s="壹";
break;
case '2':
s="贰";
break;
case '3':
s="叁";
break;
case '4':
s="肆";
break;
case '5':
s="伍";
break;
case '6':
s="陆";
break;
case '7':
s="柒";
break;
case '8':
s="捌";
break;
case '9':
s="玖";
break;
}
return s;
}
public static void main(String []args)
{
System.out.println("*************************************************************" +
" "+"欢迎使用数字转换系统"+
" "+
" "+"适用范围:99999999999.99-0.00"+
" "+
" "+"copyright:马家库"+
" *************************************************************");
new ConvertNum().setData("103056009010.13");
}
}

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/374079/viewspace-130015/,如需转载,请注明出处,否则将追究法律责任。

上一篇: IBM SOA 编程模型
请登录后发表评论 登录
全部评论