最近由於項目的原因
注意
貨幣中文說明
代碼
測試工程
static void Main(string[] args)
{
Console
string inputNum = Console
while (inputNum !=
{
//貨幣數字轉化類
NumCast nc = new NumCast();
if (nc
{
try
{
string chineseCharacter = nc
Console
}
catch (Exception er)
{
Console
}
}
else
{
Console
}
Console
inputNum = Console
}
Console
}
測試結果如下
貨幣轉化類(NumCast類)功能介紹
/// <summary>
/// 數位
/// </summary>
public enum NumLevel { Cent
/// <summary>
/// 數位的指數
/// </summary>
private int[] NumLevelExponent = new int[] {
/// <summary>
/// 數位的中文字符
/// </summary>
private string[] NumLeverChineseSign = new string[] {
/// <summary>
/// 大寫字符
/// </summary>
private string[] NumChineseCharacter = new string[] {
/// <summary>
/// 整(當沒有 角分 時)
/// </summary>
private const string EndOfInt =
/// <summary>
/// 正則表達驗證數字是否合法
/// </summary>
/// <param name=
/// <returns></returns>
public bool IsValidated<T>(T Num)
{
Regex reg = new Regex(@
if (reg
{
return true;
}
return false;
}
/// <summary>
/// 獲取數字的數位 使用log
/// </summary>
/// <param name=
/// <returns></returns>
private NumLevel GetNumLevel(double Num)
{
double numLevelLength;
NumLevel NLvl = new NumLevel();
if (Num >
{
numLevelLength = Math
for (int i = NumLevelExponent
{
if (numLevelLength >= NumLevelExponent[i])
{
NLvl = (NumLevel)i;
break;
}
}
}
else
{
NLvl = NumLevel
}
return NLvl;
}
/// <summary>
/// 是否跳位
/// </summary>
/// <returns></returns>
private bool IsDumpLevel(double Num)
{
if (Num >
{
NumLevel? currentLevel = GetNumLevel(Num);
NumLevel? nextLevel = null;
int numExponent = this
double postfixNun = Math
if(postfixNun>
nextLevel = GetNumLevel(postfixNun);
if (currentLevel != null && nextLevel != null)
{
if (currentLevel > nextLevel +
{
return true;
}
}
}
return false;
}
因為計算機不支持過長的數字
/// <summary>
/// 是否大於兆
/// 一部分是兆以前的數字
/// 另一部分是兆以後的數字
/// </summary>
/// <param name=
/// <returns></returns>
private bool IsBigThanTillion(string Num)
{
bool isBig = false;
if (Num
{
//如果大於兆
if (Num
{
isBig = true;
}
}
else
{
//如果大於兆
if (Num
{
isBig = true;
}
}
return isBig;
}
/// <summary>
/// 把數字字符串由
/// </summary>
/// <returns></returns>
private double[] SplitNum(string Num)
{
//兆的開始位
double[] TillionLevelNums = new double[
int trillionLevelLength;
if (Num
trillionLevelLength = Num
else
trillionLevelLength = Num
//兆以上的數字
TillionLevelNums[
//兆以下的數字
TillionLevelNums[
return TillionLevelNums;
}
bool isStartOfTen = false;
while (Num >=
{
if (Num ==
{
isStartOfTen = true;
break;
}
//Num的數位
NumLevel currentLevel = GetNumLevel(Num);
int numExponent = this
Num = Convert
if (currentLevel == NumLevel
{
isStartOfTen = true;
break;
}
}
return isStartOfTen;
/// <summary>
/// 合並分開的數組中文貨幣字符
/// </summary>
/// <param name=
/// <returns></returns>
private string ContactNumChinese(double[] tillionNums)
{
string uptillionStr = CalculateChineseSign(tillionNums[
string downtrillionStr = CalculateChineseSign(tillionNums[
string chineseCharactor = string
//分開後的字符是否有跳位
if (GetNumLevel(tillionNums[
{
chineseCharactor = uptillionStr + NumLeverChineseSign[(int)NumLevel
}
else
{
chineseCharactor = uptillionStr + NumLeverChineseSign[(int)NumLevel
if (downtrillionStr !=
{
chineseCharactor += NumChineseCharacter[
}
else
{
chineseCharactor +=
}
}
return chineseCharactor;
}
/// <summary>
/// 計算中文字符串
/// </summary>
/// <param name=
/// <param name=
/// <param name=
/// <returns>中文大寫</returns>
public string CalculateChineseSign(double Num
{
Num = Math
bool isDump = false;
//Num的數位
NumLevel? currentLevel = GetNumLevel(Num);
int numExponent = this
string Result = string
//整除後的結果
int prefixNum;
//余數 當為小數的時候 分子分母各乘
double postfixNun ;
if (Num >=
{
prefixNum = Convert
postfixNun = Math
}
else
{
prefixNum = Convert
postfixNun = Math
postfixNun *=
}
if (prefixNum <
{
//避免以
if (!(NumChineseCharacter[(int)prefixNum] == NumChineseCharacter[
&& currentLevel == NumLevel
{
Result += NumChineseCharacter[(int)prefixNum];
}
else
{
IsExceptTen = false;
}
//加上單位
if (currentLevel == NumLevel
{
////當為
if (NL == null)
{
Result += NumLeverChineseSign[(int)currentLevel];
//當小數點後為零時 加
if (postfixNun ==
{
Result += EndOfInt;
}
}
}
else
{
Result += NumLeverChineseSign[(int)currentLevel];
}
//當真正的個位為零時 加上
if (NL == null && postfixNun <
{
Result += NumLeverChineseSign[(int)NumLevel
}
}
else
{
//當 前綴數字未被除盡時
NumLevel? NextNL = null;
if ((int)currentLevel >= (int)(NumLevel
NextNL = currentLevel;
Result += CalculateChineseSign((double)prefixNum
if ((int)currentLevel >= (int)(NumLevel
{
Result += NumLeverChineseSign[(int)currentLevel];
}
}
//是否跳位
// 判斷是否加零
if (IsDumpLevel(Num))
{
Result += NumChineseCharacter[
isDump = true;
}
//余數是否需要遞歸
if (postfixNun >
{
Result += CalculateChineseSign(postfixNun
}
else if (postfixNun ==
{
//當數字是以零元結尾的加上 元整 比如
if (NL == null)
{
Result += NumLeverChineseSign[(int)NumLevel
Result += EndOfInt;
}
}
return Result;
}
/// <summary>
/// 外部調用的轉換方法
/// </summary>
/// <param name=
/// <returns></returns>
public string ConvertToChinese(string Num)
{
if (!IsValidated<string>(Num))
{
throw new OverflowException(
}
string chineseCharactor = string
if (IsBigThanTillion(Num))
{
double[] tillionNums = SplitNum(Num);
chineseCharactor = ContactNumChinese(tillionNums);
}
else
{
double dNum = Convert
chineseCharactor = CalculateChineseSign(dNum
}
return chineseCharactor;
}
小結
個人認為程序的靈魂是算法
是否能把需求抽象成一個好的數學模型
From:http://tw.wingwit.com/Article/program/net/201311/13101.html