using System;
using System.Text;
using System.Text.RegularExpressions;
namespace HKH.Common
{
/// <summary>
/// 人民幣大小寫格式轉換
/// </summary>
/// <remarks> Create By Lwt on 2006/09/23
/// </remarks>
public class clsRMB
{
private clsRMB()
{
}
#region 格式化
/// <summary>
/// 格式化(大寫轉小寫)
/// </summary>
/// <param name="strRMB"></param>
/// <returns></returns>
public static double Format(string strRMB)
{
try
{
//正則表達式,驗證第一位是否阿拉伯數字,確定轉換格式
//1.5億----混寫格式
if(Regex.IsMatch(strRMB,"^//d"))
{
//去掉元單位
strRMB = Regex.Replace(strRMB,"元|圓","");
char temp = strRMB[strRMB.Length - 1];
if (temp == '萬' || temp == '萬' || temp == '億')
{
return Convert.ToDouble(strRMB.Substring(0,strRMB.Length - 1)) * Math.Pow(10,GetExp(temp));
}
else
{
return Convert.ToDouble(strRMB);
}
}
//壹億伍千萬-----大寫格式
else
{
return Eval(strRMB);
}
}
catch
{
return -1;
}
}
/// <summary>
/// 格式化(小寫轉大寫)
/// </summary>
/// <param name="numRMB"></param>
/// <returns></returns>
public static string Format(double numRMB)
{
try
{
if( 0 == numRMB)
return "零元整";
StringBuilder szRMB = new StringBuilder();
//乘100以格式成整型,便於處理
ulong iRMB = Convert.ToUInt64(numRMB * 100);
szRMB.Insert(0,ToUpper(Convert.ToInt32(iRMB % 100),-2));
//去掉原來的小數位
iRMB = iRMB / 100;
int iUnit = 0;
//以每4位為一個單位段進行處理,所以下邊除以10000
while(iRMB != 0)
{
szRMB.Insert(0,ToUpper(Convert.ToInt32(iRMB % 10000),iUnit ));
iRMB = iRMB / 10000;
iUnit += 4;
}
string strRMB = szRMB.ToString();
//格式修正
strRMB = Regex.Replace(strRMB,"零+","零");
strRMB = strRMB.Replace("元零整","元整");
strRMB = strRMB.Replace("零元","元");
return strRMB.Trim('零');
}
catch
{
return "";
}
}
#endregion
#region 私有方法
/// <summary>
/// 計算表達式(大寫表達式求值)
/// </summary>
/// <param name="strRMB"></param>
/// <returns></returns>
private static double Eval(string strRMB)
{
try
{
if (null == strRMB )
return 0;
strRMB = Replace(strRMB,false);
if ("" == strRMB)
return 0;
#region 利用位權進行計算
//基礎指數
int basicExp = 0;
//當前指數
int currExp = 0;
double numRMB = 0;
for(int i = strRMB.Length - 1; i > -1 ; i --)
{
char temp = strRMB[i];
if (temp == '元' || temp == '萬' || temp == '億' || temp == '圓' || temp == '萬' )
{
basicExp = GetExp(temp);
currExp = 0;
continue;
}
else
{
if(Regex.IsMatch(temp.ToString(),"^//d"))
{
numRMB = numRMB + Convert.ToInt32(temp.ToString()) * Math.Pow(10,(basicExp + currExp));
}
else
{
currExp = GetExp(temp);
}
}
}
#endregion
return numRMB;
}
catch
{
return -1;
}
}
/// <summary>
/// 計算表達式(小寫數值求大寫字符串)
/// </summary>
/// <param name="numRMB"></param>
/// <param name="iUnit"></param>
/// <returns></returns>
private static string ToUpper(int numRMB,int iUnit)
{
try
{
if( 0 == numRMB )
{
if (iUnit == -2)
{
return "整";
}
if (iUnit == 0)
{
return "元";
}
return "零";
}
StringBuilder szRMB = new StringBuilder();
string strRMB = "";
#region 對角/分做特殊處理
if (iUnit == -2)
{
int jiao = numRMB / 10;
int fen = numRMB % 10;
if (jiao > 0)
{
szRMB.Append(jiao);
szRMB.Append(GetUnit(-1));
if ( fen > 0)
{
szRMB.Append(fen);
szRMB.Append(GetUnit(-2));
}
}
else
{
szRMB.Append(fen);
szRMB.Append(GetUnit(-2));
}
return Replace(szRMB.ToString(),true);
}
#endregion
#region 以下為整數部分正常處理
strRMB = numRMB.ToString("0000");
//前一位是否是0
bool hasZero = false;
for ( int i = 0; i < strRMB.Length; i++ )
{
//只有四位,最高位為‘千’,所以下邊的3-i為單位修正
if ( ( 3-i ) > 0)
{
if( '0' != strRMB[i] )
{
szRMB.Append(strRMB[i]);
szRMB.Append(GetUnit( 3-i ));
hasZero = false;
}
else
{
if( !hasZero )
szRMB.Append(strRMB[i]);
hasZero = true;
}
}
//最後一位,特別格式處理
//如最後一位是零,則單位應在零之前
else
{
if( '0' != strRMB[i] )
{
szRMB.Append(strRMB[i]);
szRMB.Append(GetUnit( iUnit ));
hasZero = false;
}
else
{
if (hasZero)
{
szRMB.Insert(szRMB.Length - 1,GetUnit( iUnit ));
}
else
{
szRMB.Append(GetUnit( iUnit ));
szRMB.Append(strRMB[i]);
}
}
}
}
//轉換大寫後返回
return Replace(szRMB.ToString(),true);
#endregion
}
catch
{
return "";
}
}
/// <summary>
/// 將中文大寫換成阿拉伯數字
/// </summary>
/// <param name="strRMB"></param>
/// <param name="toUpper">true--轉換為大寫/false--轉換為小寫</param>
/// <returns></returns>
private static string Replace(string strRMB,bool toUpper)
{
if(toUpper)
{
strRMB = strRMB.Replace("0","零");
strRMB = strRMB.Replace("1","壹");
strRMB = strRMB.Replace("2","貳");
strRMB = strRMB.Replace("3","三");
strRMB = strRMB.Replace("4","肆");
strRMB = strRMB.Replace("5","伍");
strRMB = strRMB.Replace("6","陸");
strRMB = strRMB.Replace("7","柒");
strRMB = strRMB.Replace("8","捌");
strRMB = strRMB.Replace("9","玖");
}
else
{
strRMB = strRMB.Replace("零","0");
strRMB = strRMB.Replace("壹","1");
strRMB = strRMB.Replace("貳","2");
strRMB = strRMB.Replace("三","3");
strRMB = strRMB.Replace("肆","4");
strRMB = strRMB.Replace("伍","5");
strRMB = strRMB.Replace("陸","6");
strRMB = strRMB.Replace("柒","7");
strRMB = strRMB.Replace("捌","8");
strRMB = strRMB.Replace("玖","9");
}
return strRMB;
}
/// <summary>
/// 獲取單位名稱
/// </summary>
/// <param name="iCode"></param>
/// <returns></returns>
private static string GetUnit(int iCode)
{
switch(iCode)
{
case -2:
return "分";
case -1:
return "角";
case 0:
return "元";
case 1:
return "拾";
case 2:
return "佰";
case 3:
return "仟";
case 4:
return "萬";
case 8:
return "億";
default:
return "";
}
}
/// <summary>
/// 獲取位權指數
/// </summary>
/// <param name="cUnit"></param>
/// <returns></returns>
private static int GetExp(char cUnit )
{
switch(cUnit)
{
case '分':
return -2;
case '角':
return -1;
case '元':
case '圓':
return 0;
case '十':
case '拾':
return 1;
case '百':
case '佰':
return 2;
case '千':
case '仟':
return 3;
case '萬':
case '萬':
return 4;
case '億':
return 8;
default:
return 0;
}
}
#endregion
}
}