將人民幣的數字表示轉化成大寫表示(C#版)

來源:互聯網
上載者:User
  將人民幣的數字表示轉化成大寫表示(C#版)     chenyu001 [收藏]
關鍵字   .net
出處  
                         將人民幣的數字表示轉化成大寫表示(C#版)   

 

 

實在沒有什麼好講的,就是一個方法,大家拿去用吧

using System;

namespace Test.Com
{
 /// <summary>
 /// 功能:字串處理函數集
 /// </summary>
 public class DealString
 {
  #region 私人成員
  /// <summary>
  /// 輸入字串
  /// </summary>
  private string inputString=null;
  /// <summary>
  /// 輸出字串
  /// </summary>
  private string outString=null;
  /// <summary>
  /// 提示資訊
  /// </summary>
  private string noteMessage=null;
  #endregion

  #region 公用屬性
  /// <summary>
  /// 輸入字串
  /// </summary>
  public string InputString
  {
   get{return inputString;}
   set{inputString=value;}
  }
  /// <summary>
  /// 輸出字串
  /// </summary>
  public string OutString
  {
   get{return outString;}
   set{outString=value;}
  }
  /// <summary>
  /// 提示資訊
  /// </summary>
  public string NoteMessage
  {
   get{return noteMessage;}
   set{noteMessage=value;}
  }
  #endregion
  
  #region 建構函式
  public DealString()
  {
   //
   // TODO: 在此處添加建構函式邏輯
   //
  }
  #endregion

  #region 公用方法
  public void ConvertToChineseNum()
  {
   string numList="零壹貳三肆伍陸柒捌玖";
   string rmbList = "分角元拾佰仟萬拾佰仟億拾佰仟萬";
   double number=0;
   string tempOutString=null;

   try
   {
    number=double.Parse(this.inputString);
   }
   catch
   {
    this.noteMessage="傳入參數非數字!";
    return;
   }
   
   if(number>9999999999999.99)
    this.noteMessage="超出範圍的人民幣值";

   //將小數轉化為整數字串
   string tempNumberString=Convert.ToInt64(number*100).ToString();
   int tempNmberLength=tempNumberString.Length;
   int i=0;
   while(i<tempNmberLength)
   {
    int oneNumber=Int32.Parse(tempNumberString.Substring(i,1));
    string oneNumberChar=numList.Substring(oneNumber,1);
    string oneNumberUnit=rmbList.Substring(tempNmberLength-i-1,1);
    if(oneNumberChar!="零")
     tempOutString+=oneNumberChar+oneNumberUnit;
    else
    {
     if(oneNumberUnit=="億"||oneNumberUnit=="萬"||oneNumberUnit=="元"||oneNumberUnit=="零")
     {
      while (tempOutString.EndsWith("零"))
      {
       tempOutString=tempOutString.Substring(0,tempOutString.Length-1);
      }

     }
     if(oneNumberUnit=="億"||(oneNumberUnit=="萬"&&!tempOutString.EndsWith("億"))||oneNumberUnit=="元")
     {
      tempOutString+=oneNumberUnit;
     }
     else
     {
      bool tempEnd=tempOutString.EndsWith("億");
      bool zeroEnd=tempOutString.EndsWith("零");
      if(tempOutString.Length>1)
      {
       bool zeroStart=tempOutString.Substring(tempOutString.Length-2,2).StartsWith("零");
       if(!zeroEnd&&(zeroStart||!tempEnd))
        tempOutString+=oneNumberChar;
      }
      else
      {
       if(!zeroEnd&&!tempEnd)
        tempOutString+=oneNumberChar;
      }
     }
    }
    i+=1;
   }

   while (tempOutString.EndsWith("零"))
   {
    tempOutString=tempOutString.Substring(0,tempOutString.Length-1);
   }

   while(tempOutString.EndsWith("元"))
   {
    tempOutString=tempOutString+"整";
   }

   this.outString=tempOutString;

   
  }
  #endregion
 }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.