科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道将一个BigDecimal类型的数转换成中文

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

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

  public class toChinese   {   public toChinese()   {   }      public static String ConvetC(BigDecimal bds)   {   String strZ

作者:中国IT实验室 来源:中国IT实验室 2007年9月25日

关键字: 编程 java

  • 评论
  • 分享微博
  • 分享邮件

  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));
  }
  
  }

查看本文来源

    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章