[分享]转换金额为人民币大写代码

         /// <summary>
        /// 转换金额为人民币大写
        /// </summary>
        /// <param name="n">Int64 金额</param>
        /// <returns>人民币大写</returns>       
        public static string ToRMB(double dn)
        {
            string[] strN = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
            string[] strC = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
            string[] strA = { "", "圆", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "万亿", "拾", "佰", "仟", "亿亿", };
            int[] nLoc = { 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 };

            string strFrom = "";
            string strTo = "";
            string strChar;
            int m, mLast = -1, nCount = 0;

            if (strFrom.Length > strA.GetUpperBound(0) - 1) return "***拜托,这么多钱还需要数吗***";

            if (dn < 0)
            {
                dn *= -1;
                strTo = "负";
            }

            Int64 n1 = (Int64)dn;                   // 元
            strFrom = n1.ToString();

            for (int i = strFrom.Length; i > 0; i--)
            {
                strChar = strFrom.Substring(strFrom.Length - i, 1);
                m = Convert.ToInt32(strChar);
                if (m == 0)
                {
                    // 连续为0时需要补齐中间单位,且只补第一次
                    if (nLoc[i] > 0 && nCount == 0 && strFrom.Length > 1)
                    {
                        strTo = strTo + strA[i];
                        nCount++;
                    }
                }
                else
                {
                    // 补0
                    if (mLast == 0)
                    {
                        strTo = strTo + strC[0];
                    }

                    // 数字转换为大写
                    strTo = strTo + strC[m];
                    // 补足单位
                    strTo = strTo + strA[i];
                    nCount = 0;
                }
                mLast = m;
            }

            Int64 n2 = ((Int64)(dn * 100)) % 100;   // 角分
            Int64 n3 = n2 / 10;                     // 角
            Int64 n4 = n2 % 10;                     // 分
            string s2 = "";

            if (n4 > 0)
            {
                s2 = strC[n4] + "分";
                if (n3 > 0)
                {
                    s2 = strC[n3] + "角" + s2;
                }
            }
            else
            {
                if (n3 > 0)
                {
                    s2 = strC[n3] + "角";
                }
            }
            strTo = strTo + s2;

            if (strTo == "") strTo = strC[0];                   // 全0显示为零
            else if (s2 == "") strTo = strTo + "整";            // 无角分显示整
            return strTo;
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值