asp教程.net 可達千百萬億 人民幣小寫轉大寫金額
/// <summary>
/// 小寫金額轉換成大寫金額
/// </summary>
/// <param name="number"></param>
/// <param name="turntype"></param>
/// <returns></returns>
public static string converttochnmoney(decimal number, chnmoneytype turntype)
{
string s_chnnumstring = "零壹貳三肆伍陸柒捌玖";
//可正確最大金額可以到千萬億元正,有分位為百萬億元或十萬億
string result = string.empty;
if (number > 0)
{
string s_dxdw = "分角元拾佰仟萬拾佰仟億拾佰仟萬拾佰仟";
ulong je = (ulong)(number * 100);
ulong temnum = je;
int index = 0;
for (int n = 0; temnum != 0; n++)
{
index = (int)(temnum - temnum / 10 * 10); //取出最後一個數
temnum = temnum / 10;
result = s_chnnumstring.substring(index, 1) + s_dxdw.substring(n, 1) + result; //發票格式
}
if (turntype == chnmoneytype.checktype)
{
string temresult = string.empty;
string temmoney = string.empty;
string unit = string.empty;
string digitchar = string.empty;
string str1 = "零元萬整";
string str2 = "零元萬億整";
for (int n = result.length - 2; n >= 0; n = n - 2) //從後面每二個取字串。
{
temmoney = result.substring(n, 2);
unit = temmoney.substring(1, 1);
digitchar = temmoney.substring(0, 1);
switch (unit)
{
case "分":
if (digitchar == "零")
temmoney = "整";
break;
case "元":
case "萬":
case "億":
string temdigitchar = temresult.substring(0, 1);
if (digitchar == "零")
{
if (str1.indexof(temdigitchar) == -1)
temmoney = unit + "零";
else
{
if (temdigitchar == "萬")
temresult = temresult.substring(1);
temmoney = unit;
}
}
else if (temdigitchar == "萬")
temresult = temresult.substring(1);
break;
default:
if (digitchar == "零")
{
if (str2.indexof(temresult.substring(0, 1)) != -1)
temmoney = "";
else
temmoney = "零";
}
break;
}
temresult = temmoney + temresult;
}
result = temresult;
}
}
return result;
}
}
/// <summary>
/// 大字金額的格式。
/// </summary>
public enum chnmoneytype
{
/// <summary>
/// 支票格式
/// </summary>
checktype,
/// <summary>
/// 發票格式
/// </summary>
invoicetype
}