您的位置:首页 > 数据库

金额转换成大写(从Sql Server版转换成的C#版)

2007-05-15 19:52 543 查看
namespace AA
{
public class BB
{
public static string L2U(decimal lowerMoney)
{
string lowerStr = string.Empty;
string upperPart = string.Empty;
string upperStr = string.Empty;

lowerStr = lowerMoney.ToString("F2");

for (int index = 0; index < lowerStr.Length; index++)
{
upperPart = GetUpperNumber(lowerStr.Substring(lowerStr.Length - index-1, 1))
+ GetUnit(index+1);
upperStr = upperPart + upperStr;
}

upperStr = upperStr.Replace("零拾", "零");
upperStr = upperStr.Replace("零佰", "零");
upperStr = upperStr.Replace("零仟", "零");
upperStr = upperStr.Replace("零零零", "零");
upperStr = upperStr.Replace("零零", "零");
upperStr = upperStr.Replace("零角零分", "整");
upperStr = upperStr.Replace("零分", "整");
upperStr = upperStr.Replace("零角", "零");
upperStr = upperStr.Replace("零亿零万零元", "亿元");
upperStr = upperStr.Replace("亿零万零元", "亿元");
upperStr = upperStr.Replace("零亿零万", "亿");
upperStr = upperStr.Replace("零万零元", "万元");
upperStr = upperStr.Replace("万零元", "万元");
upperStr = upperStr.Replace("零亿", "亿");
upperStr = upperStr.Replace("零万", "万");
upperStr = upperStr.Replace("零元", "元");
upperStr = upperStr.Replace("零零", "零");

if ("元" == upperStr.Substring(0, 1))
{
upperStr = upperStr.Substring(1, upperStr.Length - 1);
}
if ("零" == upperStr.Substring(0, 1))
{
upperStr = upperStr.Substring(1, upperStr.Length - 1);
}
if ("角" == upperStr.Substring(0, 1))
{
upperStr = upperStr.Substring(1, upperStr.Length - 1);
}
if ("分" == upperStr.Substring(0, 1))
{
upperStr = upperStr.Substring(1, upperStr.Length - 1);
}
if ("整" == upperStr.Substring(0, 1))
{
upperStr = "零元整";
}

if ("壹拾" == upperStr.Substring(0, 2))
{
upperStr = upperStr.Substring(1, upperStr.Length - 1);
}

return upperStr;
}

private static string GetUpperNumber(string str)
{
string cstr = string.Empty;
switch (str)
{
case ".": cstr = "元"; break;
case "0": cstr = "零"; break;
case "1": cstr = "壹"; break;
case "2": cstr = "贰"; break;
case "3": cstr = "叁"; break;
case "4": cstr = "肆"; break;
case "5": cstr = "伍"; break;
case "6": cstr = "陆"; break;
case "7": cstr = "柒"; break;
case "8": cstr = "捌"; break;
case "9": cstr = "玖"; break;
}

return cstr;
}

private static string GetUnit(int index)
{
string cstr;
switch (index)
{
case 1: cstr = "分"; break;
case 2: cstr = "角"; break;
case 3: cstr = ""; break;
case 4: cstr = ""; break;
case 5: cstr = "拾"; break;
case 6: cstr = "佰"; break;
case 7: cstr = "仟"; break;
case 8: cstr = "万"; break;
case 9: cstr = "拾"; break;
case 10: cstr = "佰"; break;
case 11: cstr = "仟"; break;
case 12: cstr = "亿"; break;
case 13: cstr = "拾"; break;
case 14: cstr = "佰"; break;
case 15: cstr = "仟"; break;
case 16: cstr = "万"; break;
default: cstr = ""; break;
}

return cstr;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐