#region ChangeMoneyToChinese /// <summary> /// 把金額小寫轉換爲中文大寫 /// </summary> /// <param name="Money"></param> /// <returns></returns> public static string ChangeMoneyToChinese(decimal MoneyValue) { string sCHNumber="", sCHMoneyUnit="", sMoney="", sResultName=""; int iMoneyLength=0, iNumber=0, iUnit=0, iLengthTwo=0, iUnitTwo=0; bool bExecute; // //初始化值 // sCHNumber = "壹貳叄肆伍陸柒捌玖"; sCHMoneyUnit = "分角元拾佰仟萬拾佰仟億拾佰仟"; sMoney = MoneyValue.ToString("###.00"); iMoneyLength = sMoney.Length; if(iMoneyLength >=16 || MoneyValue < 0.01m) { return ""; } // //取無小數的字符串 // sMoney = sMoney.Replace(".",""); iMoneyLength = iMoneyLength - 1; iNumber = iMoneyLength * 2 - 1; iUnit = int.Parse(sMoney.Substring(0,1)) * 2 - 1; // //若是金額<1 sResultName='' // if((iMoneyLength == 3) && (iUnit < 0)) sResultName = ""; else { // //若是金額>=1,轉換金額 // if(iUnit > 0) { sResultName = SubStringByte(sCHNumber,iUnit-1,2) + SubStringByte(sCHMoneyUnit,iNumber-1,2); } } iLengthTwo = 2; iNumber = iNumber - 2; bExecute = true; while(iLengthTwo <= iMoneyLength && bExecute) { iUnitTwo = int.Parse(sMoney.Substring(iLengthTwo-1, 1)) * 2 - 1; if(iUnitTwo > 0) { sResultName = sResultName + SubStringByte(sCHNumber,iUnitTwo-1, 2) + SubStringByte(sCHMoneyUnit,iNumber-1, 2); iLengthTwo = iLengthTwo + 1; iNumber = iNumber - 2; continue; } int iCount = 0; while (iUnitTwo < 0) { if(iMoneyLength >= 11) { if(iNumber < 21) iCount = iCount + 1; } if(iNumber == 5 || (iNumber == 13 && iCount <= 3) || iNumber == 21) { sResultName=sResultName+SubStringByte(sCHMoneyUnit,iNumber-1, 2); } if(iNumber == 1) { sResultName = sResultName + "整"; bExecute = false; break; } iLengthTwo = iLengthTwo + 1; iNumber = iNumber - 2; iUnitTwo = int.Parse(sMoney.Substring(iLengthTwo-1, 1)) * 2 - 1; if(iUnitTwo < 0) { continue; } else { if(iMoneyLength == 3) sResultName = sResultName + "零"; else sResultName = sResultName + "零"; } } } // //去掉元和角之間零(1230.32) // int iYuanPos = sResultName.IndexOf("元",0)+1; int iJaoPos = sResultName.IndexOf("角",0)+1; if(iYuanPos != -1 && iJaoPos != -1) { int iZeroPos = sResultName.IndexOf("零",iYuanPos); if(iZeroPos != -1) { sResultName=sResultName.Remove(iZeroPos,1); } } return sResultName; } /// <summary> /// 按字符串取值 /// </summary> /// <param name="Source"></param> /// <param name="Start"></param> /// <param name="Length"></param> /// <returns></returns> private static string SubStringAll(string Source,int Start,int Length) { string sResult=""; if(Start>=Source.Length) { sResult=""; } else { if(Start+Length>Source.Length) { Length=Source.Length-Start; } sResult=Source.Substring(Start,Length); } return sResult; } /// <summary> /// 按字節取值 /// </summary> /// <param name="Source"></param> /// <param name="Start"></param> /// <param name="Length"></param> /// <returns></returns> private static string SubStringByte(string Source,int Start,int Length) { System.Text.UnicodeEncoding UnicodeArray =new System.Text.UnicodeEncoding(); byte[] bArray =UnicodeArray.GetBytes(Source); string sResult=""; if(Start>=bArray.Length) { sResult=""; } else { if(Start+Length>bArray.Length) { Length=Source.Length-Start; } sResult=UnicodeArray.GetString(bArray,Start,Length); } return sResult; } #endregion