在做涉及到金額的內容時,通常是需要輸出一個金額的大寫形式,這是中國的傳統哦,嘿嘿,同時也提高安全性。
實現人民幣從小寫到大寫的方法有很多種,今天,我這裡提供一種給大家共用
一部分代碼展示:
public static string Convert(decimal number){ bool NegativeFlag = false; decimal RMBNumber; CheckNumberLimit(number); RMBNumber = Math.Round(number, 2); //將?四?舍?五?入?取?位?小?數? if (RMBNumber == 0) { return "零?元?整?"; } else if (RMBNumber < 0) //如?果?是?負?數? { NegativeFlag = true; RMBNumber = Math.Abs(RMBNumber); //取?絕?對?值? } else { NegativeFlag = false; } string buf = ""; // 存?放?返?回?結?果? string strDecPart = ""; // 存?放?小?數?部?分?的?處?理?結?果? string strIntPart = ""; // 存?放?整?數?部?分?的?處?理?結?果? string[] tmp = null; string strDigital = RMBNumber.ToString(); tmp = strDigital.Split(cDelim, 2); // 將?數?據?分?為?整?數?和?小?數?部?分? if (RMBNumber >= 1m) // 大?於?時?才?需?要?進?行?整?數?部?分?的?轉?換? { strIntPart = ConvertInt(tmp[0]); } if (tmp.Length > 1) //分解出了小數 { strDecPart = ConvertDecimal(tmp[1]); } else //沒有小數肯定是為整 { strDecPart = "整"; } if (NegativeFlag == false) //是?否?負?數? { buf = strIntPart + strDecPart; } else { buf = "負" + strIntPart + strDecPart; } return buf;}
使用的方法非常簡單:
Console.WriteLine("¥12345.67 對應的大寫金額是 :" + DeltaCat.Net.RMB.Convert(12345.67M));
demo代碼[c#人民幣小寫轉大寫]