c#实现人民币小写转大写(原)

    前段时间有个朋友在做一个关于金融的项目,提到金融中关于钱的小写转大写的问题,虽然网络上也有很多这方面的,但是测试过后仍然存在bug,于是没事就演了一下,效果还不错,暂时还没有发现错误,如果你发现了,可以联系我哦,交流切磋下嘛,呵呵!
public class format
{
private readonly string _NumChinese="零壹贰叁肆伍陆柒捌玖";
private string GetNumCHN( decimal number )
{
 
if ( Math.Abs( number ) < (decimal)0.01  )
  
return "零圆整";
 
long y = Convert.ToInt64( Decimal.Truncate( number / 100000000 ) );
 
long w = Convert.ToInt64( Decimal.Truncate( (number - y * 100000000)/10000 ) );
 
long g = Convert.ToInt64( Decimal.Truncate( number % 10000 ) );
 
long x = Convert.ToInt64( Decimal.Truncate( ( number - Decimal.Truncate( number ) ) * 100 ) ); 

 
string re;
 
if ( y < 1000 && y != 0 )
  re 
= GetCHN( y ).Substring( 1 ) + "亿";
 
else
  re 
= GetCHN( y ); 

 
if ( w != 0 )
 {
  
if ( re != "" )
   re 
+= GetCHN( w ) + "";
  
else
   re 
= GetCHN( w ).Substring( 1 );
 } 

 
if ( g != 0 )
 {
  
if ( re != "" )
   re 
+= GetCHN( g );
  
else
   re 
= GetCHN( g ).Substring( 1 );
 } 

 
if ( re != "" )  //高位有数值
 {
  
if ( x == 0 ) //无小数位
   re += "圆整";
  
else   //有小数位
   re += "圆零" + GetFCHN( x );
 }
 
else    //高位无数值
 {
  
if ( x == 0 ) //无小数位
   re += "零圆";
  
else   //有小数位
   re += GetFCHN( x );
 } 

 
return re;


private string GetFCHN( long aa )
{
 
string re = string.Empty;
 
int x = Convert.ToInt32(aa % 10);  //
 int y = Convert.ToInt32((aa - x)/10); //角 

 
if ( y != 0 )
  re 
= _NumChinese[y] + ""

 
if ( x != 0 )
  re 
+= _NumChinese[x] + ""

 
return re;


private string GetCHN( long aa )
{
 
string re = string.Empty; 

 
if ( aa == 0 )
  
return re; 

 
int l1 = Convert.ToInt32( aa % 10 );
 
int l2 = Convert.ToInt32( (aa - l1)/10 % 10 );
 
int l3 = Convert.ToInt32( (aa - l1 - l2 * 10 )/100 % 10 );
 
int l4 = Convert.ToInt32( Math.Floor( aa / 1000 ) ); 

 re 
= "";
 
if ( l4 == 0 )
 {
  
if ( re[re.Length-1!= '' && l3 != 0 )
   re 
+= "";
 }
 
else
  re 
+= _NumChinese[l4] + ""

 
if ( l3 == 0 )
 {
  
if ( re[re.Length-1!= '' && l2 != 0 )
   re 
+= "";
 }
 
else
  re 
+= _NumChinese[l3] + ""

 
if ( l2 == 0 )
 {
  
if ( re[re.Length-1!= '' && l1 != 0 )
   re 
+= "";
 }
 
else
  re 
+= _NumChinese[l2] + ""

 
if ( l1 != 0 ) re += _NumChinese[l1]; 

 
return re;
}

}

直接调用GetNumCHN( decimal number ) 方法,填写入处理的数据金额,得到的就是大写的金额了。

思想:把整个数字,分为8位一段,以“亿”阁开,8位以内,分成4位阁开,以万连接,最后剩下的就是四位,四位的万以内的数据了,把这些数据最后分别连接起来,再加上小数部分的处理,得到大写的结果了。

posted on 2007-02-13 14:01  网前到黑  阅读(523)  评论(1)    收藏  举报

导航