当前位置导航:炫浪网>>网络学院>>编程开发>>JAVA教程>>Java进阶

将一个BigDecimal类型的数转换成中文


  public class toChinese
  {
  public toChinese()
  {
  }
  
  public static String ConvetC(BigDecimal bds)
  {
  String strZero = "零";
  String strYuan = "圆";
  String strJiao = "角";
  String strFen = "分";
  String strZheng = "整";
  String strItem[] = { "", "万", "亿", "兆", "??", "!!", "**"};//萬
  String strC[]={ "", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖", "拾"};
  String strB[]={ "", "拾", "佰", "仟"};
  
  String strChinese = new String("");
  String strSource = bds.toString();
  String[] strDesk;
  String strPrec = new String("");
  int i = 0 , j = 0, iNumber = 0, iPrec = 0, iItem = 0, iLoc = 0;
  iNumber = strSource.indexOf(".");
  strPrec = strSource.substring(iNumber+1);
  //System.out.println(strPrec);
  iPrec = strSource.length() - iNumber - 1;
  
  iItem = (int)((iNumber+3)/4);
  strDesk = new String[iItem];
  //System.out.println( "iNumber = " + iNumber + "iPrec = " + iPrec + "iItem = " + iItem);
  
  for(i=0; i<iItem; i++)
  {
  iLoc = (iNumber - 4 * i) >4 ? 4 : (iNumber - 4 * i);
  //System.out.println( "iNumber = " + iNumber + "iPrec = " + iPrec + "iItem = " + iItem + "iLoc = " + iLoc);
  strDesk[i] = "";
  int iNowItem[] = { -1, -1, -1, -1};
  for(j= (i*4)+iLoc-1; j> (i*4)-1 ; j--)
  {
  iNowItem[j-i*4] = (int)(strSource.charAt(iNumber - j - 1))-48;
  //System.out.println(j-i*4 + " " + strSource.charAt(iNumber - j - 1) + " " + j);
  
  }
  strDesk[i] = strItem[i];
  //System.out.println(strDesk[i]);
  if(iNowItem[0]>0)
  {
  strDesk[i] = strC[iNowItem[0]] + strB[0] + strDesk[i];
  }
  
  if(iNowItem[1]>0)
  {
  strDesk[i] = strC[iNowItem[1]] + strB[1] + strDesk[i];
  }
  else if(iNowItem[1]==0 && iNowItem[0]>0 )
  {
  strDesk[i] = strZero + strDesk[i];
  }
  
  if(iNowItem[2]>0)
  {
  strDesk[i] = strC[iNowItem[2]] + strB[2] + strDesk[i];
  }
  else if(iNowItem[2]==0 && iNowItem[1]>0 )
  {
  strDesk[i] = strZero + strDesk[i];
  }
  
  if(iNowItem[3]>0)
  {
  strDesk[i] = strC[iNowItem[3]] + strB[3] + strDesk[i];
  }
  else if(iNowItem[3]==0 && iNowItem[2]>0 )
  {
  strDesk[i] = strZero + strDesk[i];
  }
  if(iNowItem[0]==0&&iNowItem[1]==0&&iNowItem[2]==0&&iNowItem[3]==0)
  {
  strDesk[i] = strZero;
  if(i==0)
  {
  strDesk[i] = "";
  }
  else if (strDesk[i-1].equals("") || strDesk[i-1].indexOf(strZero)==0)
  {
  strDesk[i] = "";
  }
  }
  strChinese = strDesk[i] + strChinese ;
  }
  strChinese = strChinese + strYuan;
  
  int iJiao=0, iFen=0;
  try
  {
  iJiao = (int)(strPrec.charAt(0))-48;
  }
  catch(Exception e1)
  {
  }
  try
  {
  iFen = (int)(strPrec.charAt(1))-48;
  }
  catch(Exception e2)
  {
  }
  if(iJiao>0)
  {
  strChinese = strChinese + strC[iJiao] + strJiao ;
  }
  if(iFen>0)
  {
  strChinese = strChinese + strC[iFen] + strFen ;
  }
  else
  strChinese = strChinese + strZheng ;
  //System.out.println(strChinese);
  return(strChinese);
  }
  public static void main(String[] args)
  {
  BigDecimal bdtest = new BigDecimal(756857867585656.09);
  System.out.println(toChinese.ConvetC(bdtest));
  }
  
  }
相关内容
赞助商链接