博主喝口茶,一毛也是爱

收缩

C# 金额转换成大写

106 人阅读
分类:

做报表时候一般金额会使用大写来显示,这里将金额转换成大写格式

/// <summary>
/// 大写金额实体
/// </summary>
public class OrderReportStatisticsMoneyModel
{
    /// <summary>
    /// 分位
    /// </summary>
    public string MinVal { get; set; }
    /// <summary>
    /// 角位
    /// </summary>
    public string DimeVal { get; set; }
    /// <summary>
    /// 元位
    /// </summary>
    public string DollarVal { get; set; }
    /// <summary>
    /// 拾位
    /// </summary>
    public string TenVal { get; set; }
    /// <summary>
    /// 佰位
    /// </summary>
    public string HundredVal { get; set; }
    /// <summary>
    /// 仟位
    /// </summary>
    public string ThousandVal { get; set; }
    /// <summary>
    /// 万位
    /// </summary>
    public string TenThousandVal { get; set; }
    /// <summary>
    /// 拾万位
    /// </summary>
    public string MillionVal { get; set; }
}

转换方法

/// <summary>
/// 转换成大写金额(暂不统计负数)
/// </summary>
/// <param name="money"></param>
/// <returns></returns>
public static OrderReportStatisticsMoneyModel GetCapitalMoney(decimal money)
{
    var model = new OrderReportStatisticsMoneyModel();
    if (money > 0)
    {
        //去除金额无效的小数零
        string moneyVal = money.ToString("0.##");
        //分别统计小数点前面和后面金额
        string[] moneySplit = moneyVal.Split('.');
        if (moneySplit.Length > 0)
        {
            //统计小数点前面金额
            string preMoney = moneySplit[0];
            int iTemp = 0;
            while (iTemp < preMoney.Length)
            {
                string moneyCapitalVal = "零";
                int currentMoneyLength = preMoney.Substring(iTemp).Length;

                switch (preMoney.Substring(iTemp, 1))
                {
                    case "0":
                        moneyCapitalVal = "零";
                        break;
                    case "1":
                        moneyCapitalVal = "壹";
                        break;
                    case "2":
                        moneyCapitalVal = "贰";
                        break;
                    case "3":
                        moneyCapitalVal = "叁";
                        break;
                    case "4":
                        moneyCapitalVal = "肆";
                        break;
                    case "5":
                        moneyCapitalVal = "伍";
                        break;
                    case "6":
                        moneyCapitalVal = "陆";
                        break;
                    case "7":
                        moneyCapitalVal = "柒";
                        break;
                    case "8":
                        moneyCapitalVal = "捌";
                        break;
                    case "9":
                        moneyCapitalVal = "玖";
                        break;
                }
                switch (currentMoneyLength)
                {
                    case 6:
                        model.MillionVal = moneyCapitalVal;
                        break;
                    case 5:
                        model.TenThousandVal = moneyCapitalVal;
                        break;
                    case 4:
                        model.ThousandVal = moneyCapitalVal;
                        break;
                    case 3:
                        model.HundredVal = moneyCapitalVal;
                        break;
                    case 2:
                        model.TenVal = moneyCapitalVal;
                        break;
                    case 1:
                        model.DollarVal = moneyCapitalVal;
                        break;
                    default:
                        break;
                }

                iTemp++;
            }
        }
        if (moneySplit.Length > 1)
        {
            //统计小数点后面金额
            string suffixMoney = (moneySplit[1] + "00").Substring(0, 2);
            int iTemp = 0;
            while (iTemp < suffixMoney.Length)
            {
                string moneyCapitalVal = "零";
                int currentMoneyLength = suffixMoney.Substring(iTemp).Length;

                switch (suffixMoney.Substring(iTemp, 1))
                {
                    case "0":
                        moneyCapitalVal = "零";
                        break;
                    case "1":
                        moneyCapitalVal = "壹";
                        break;
                    case "2":
                        moneyCapitalVal = "贰";
                        break;
                    case "3":
                        moneyCapitalVal = "叁";
                        break;
                    case "4":
                        moneyCapitalVal = "肆";
                        break;
                    case "5":
                        moneyCapitalVal = "伍";
                        break;
                    case "6":
                        moneyCapitalVal = "陆";
                        break;
                    case "7":
                        moneyCapitalVal = "柒";
                        break;
                    case "8":
                        moneyCapitalVal = "捌";
                        break;
                    case "9":
                        moneyCapitalVal = "玖";
                        break;
                }
                switch (currentMoneyLength)
                {
                    case 2:
                        model.DimeVal = moneyCapitalVal;
                        break;
                    case 1:
                        model.MinVal = moneyCapitalVal;
                        break;
                    default:
                        break;
                }

                iTemp++;
            }
        }
    }

    model.MinVal = string.IsNullOrEmpty(model.MinVal) ? "零" : model.MinVal;
    model.DimeVal = string.IsNullOrEmpty(model.DimeVal) ? "零" : model.DimeVal;
    model.DollarVal = string.IsNullOrEmpty(model.DollarVal) ? "零" : model.DollarVal;
    model.TenVal = string.IsNullOrEmpty(model.TenVal) ? "零" : model.TenVal;
    model.HundredVal = string.IsNullOrEmpty(model.HundredVal) ? "零" : model.HundredVal;
    model.ThousandVal = string.IsNullOrEmpty(model.ThousandVal) ? "零" : model.ThousandVal;
    model.TenThousandVal = string.IsNullOrEmpty(model.TenThousandVal) ? "零" : model.TenThousandVal;
    model.MillionVal = string.IsNullOrEmpty(model.MillionVal) ? "零" : model.MillionVal;

    return model;
}

调用生成对应大写金额

decimal moneyVal = 23456.78M;
var capitalMoney = GetCapitalMoney(moneyVal);

string capitalVal = string.Format("{0}  拾   {1}  万   {2}  仟   {3}  佰   {4}  拾   {5}  元   {6}  角   {7}  分",
                    capitalMoney.MillionVal, capitalMoney.TenThousandVal
                    , capitalMoney.ThousandVal, capitalMoney.HundredVal
                    , capitalMoney.TenVal, capitalMoney.DollarVal
                    , capitalMoney.DimeVal, capitalMoney.MinVal);
Console.WriteLine(capitalVal);
Console.ReadKey();

tt.jpg



和博主交个朋友吧
    发布篇幅
    • 文章总数:0
    • 原创:0
    • 转载:0
    • 译文:0
    文章分类
      文章存档
      阅读排行