c# 金额小写转大写(财务金额)

public static string NumtoChinese(decimal s)
{
    s = Math.Round(s, 2);//四舍五入到两位小数,即分
    string[] n = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
    //数字转大写
    string[] d = { "", "分", "角", "元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿" };
    //不同位置的数字要加单位
    List<string> needReplace = new List<string> { "零拾", "零佰", "零仟", "零万", "零亿", "亿万", "零元", "零零", "零角", "零分" };
    List<string> afterReplace = new List<string> { "零", "零", "零", "万", "亿", "亿", "元", "零", "", "" };//特殊情况用replace剔除
    string e = s % 1 == 0 ? "整" : "";//金额是整数要加一个“整”结尾
    string re = "";
    Int64 a = (Int64)(s * 100);
    int k = 1;
    while (a != 0)
    {//初步转换为大写+单位
        re = n[a % 10] + d[k] + re;
        a = a / 10;
        k = k < 11 ? k + 1 : 4;
    }
    string need = needReplace.Where(tb => re.Contains(tb)).FirstOrDefault<string>();
    while (need != null)
    {
        int i = needReplace.IndexOf(need);
        re = re.Replace(needReplace[i], afterReplace[i]);
        need = needReplace.Where(tb => re.Contains(tb)).FirstOrDefault<string>();
    }//循环排除特殊情况
    re = re == "" ? "" : re + e;
    return re;
}

  

posted on 2022-07-08 18:03  itjeff  阅读(262)  评论(0编辑  收藏  举报

导航