数字转换成人民币大写字符

C# 专栏收录该内容
2 篇文章 0 订阅
        private static string MoneyNum = "零一二三四五六七八九";
        private static string MoneyUnit = "十百千万十百千亿";
        private static string CashUnit = "元角分整";
        private static string[] BeforeReplace = new string[] {  MoneyNum[0].ToString() + MoneyNum[0].ToString(),
                                                                MoneyUnit[7].ToString() + MoneyNum[0].ToString() + MoneyUnit[3].ToString() ,
                                                                MoneyNum[0].ToString() + MoneyUnit[3].ToString(),
                                                                MoneyNum[0].ToString() + MoneyUnit[7].ToString(),
                                                                MoneyUnit[7].ToString() + MoneyUnit[3].ToString(),
                                                                MoneyNum[0].ToString() + MoneyNum[0].ToString()
                                                             };
        private static string[] AfterReplace = new string[] {  MoneyNum[0].ToString(),
                                                                MoneyUnit[7].ToString() + MoneyNum[0].ToString(),
                                                                MoneyUnit[3].ToString() + MoneyNum[0].ToString(),
                                                                MoneyUnit[7].ToString() + MoneyNum[0].ToString(),
                                                                MoneyUnit[7].ToString() + MoneyNum[0].ToString(),
                                                                MoneyNum[0].ToString()
                                                             };

        /// <summary>
        /// 字符串替换方法
        /// Create By HJ 2007-10-21
        /// </summary>
        /// <param name="moneyNum"></param>
        /// <returns></returns>
        public static string getCash(decimal moneyNum)
        {
            string intNum, point;
            intNum = Convert.ToString(Math.Floor(moneyNum));       ///取整数部分
            point = Convert.ToString(Math.Floor(moneyNum * 100));  ///取小数部分
            point = point.Substring(point.Length - 2);

            ///计算整数部分
            for (int i = 0; i < MoneyNum.Length; i++)
            {
                intNum = intNum.Replace(i.ToString(), MoneyNum[i].ToString());
            }
            int intNumLength = intNum.Length;
            for (int i = intNumLength - 1; i > 0; i--)
            {   ///根据位数把单位加上
                ///如果是零则不加单位,但是 万  和  亿 需要加上
                if (intNum[i - 1] == MoneyNum[0] && (intNumLength - i + 7) % 8 != 3 && (intNumLength - i + 7) % 8 != 7) continue;
                intNum = intNum.Insert(i, MoneyUnit[(intNumLength - i + 7) % 8].ToString());
            }
            ///替换  零零 -> 零  亿零万  ->  亿零,零万  ->  万零,零亿 -> 亿零,亿万 -> 亿零,零零,再调用一次,确保 亿零万 替换后的情况
            for (int i = 0; i < BeforeReplace.Length; i++)
            {
                while(intNum.IndexOf(BeforeReplace[i])>-1)
                {
                    intNum = intNum.Replace(BeforeReplace[i], AfterReplace[i]);
                }
            }

            if (intNum.EndsWith(MoneyNum[0].ToString())) intNum = intNum.Substring(0, intNum.Length - 1);
            intNum += CashUnit[0].ToString();

            ///计算小数部分
            point = MoneyNum.Substring(Convert.ToInt16(point[0].ToString()), 1) + MoneyNum.Substring(Convert.ToInt16(point[1].ToString()), 1);
            if (point == MoneyNum[0].ToString() + MoneyNum[0].ToString())
            {///零角零分则不写
                point = String.Empty;
            }
            else if (point.EndsWith(MoneyNum[0].ToString()))
            {///某角零分去掉后面的零
                point = point.Substring(0, 1) + CashUnit[1].ToString();
            }
            else
            {
                point = point.Substring(0, 1) + CashUnit[1].ToString() + point.Substring(1, 1) + CashUnit[2].ToString();
            }

            return intNum + point + CashUnit[3].ToString();
        }
 

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页

打赏作者

hj3793

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值