C# 人民币大写转换

人民币大写转换

/// <summary>
/// 人民币大写转换帮助类
/// </summary>
internal class RMBUppercaseHelper
{
    private const string CN_ZERO = "零";
    private const string CN_DOLLAR = "元";
    private const string CN_INTEGER = "整";
    private const string REG_NUMBER_COMMA_POINT = @"^[0-9,.]+$";
    private const string REG_CURRENCY = @"^((\d{1,3}(,\d{3})*(.((\d{3},)*\d{1,3}))?)|(\d+(.\d+)?))$";

    /// <summary>
    /// 中文数字
    /// </summary>
    private static readonly string[] chineseDigits = new string[] { CN_ZERO, "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };

    /// <summary>
    /// 中文小单位
    /// </summary>
    private static readonly string[] chineseSmallUnits = new string[] { string.Empty, "拾", "佰", "仟" };

    /// <summary>
    /// 中文大单位
    /// </summary>
    private static readonly string[] chineseBigUnits = new string[] { string.Empty, "万", "亿", "兆", "京", "垓", "秭", "穰", "沟", "涧", "正", "载" };

    /// <summary>
    /// 中文小数单位
    /// </summary>
    private static readonly string[] chineseDecimalsUnits = new string[] { "角", "分", "厘", "毫", "丝", "忽", "微" };

    /// <summary>
    /// 转换
    /// </summary>
    /// <param name="money">金额</param>
    /// <param name="message">提示消息</param>
    /// <returns></returns>
    public static string Converter(string money, out string message)
    {
        message = string.Empty;
        if (string.IsNullOrWhiteSpace(money))
        {
            message = "金额不能为空";
            return string.Empty;
        }

        if (!Regex.IsMatch(money, REG_NUMBER_COMMA_POINT))
        {
            message = "金额中存在无效字符";
            return string.Empty;
        }

        if (!Regex.IsMatch(money, REG_CURRENCY))
        {
            // 
            message = "金额字符串格式错误";
            return string.Empty;
        }

        // 删除所有逗号
        money = Regex.Replace(money, @",", string.Empty);

        // 替换掉所有以0开头的数字
        money = Regex.Replace(money, @"^0+", string.Empty);


        decimal inputMoney = 0;

        if (!decimal.TryParse(money, out inputMoney))
        {
            message = "不是有效的金额";
            return string.Empty;
        }

        // 整数部分
        string integerPart;

        // 小数部分
        string fractionPart;

        // 拆分金额数据
        if (!SplitMoney(inputMoney, out integerPart, out fractionPart))
        {
            message = $"金额拆分错误";
            return string.Empty;
        }

        string outputCharacters = "";

        if (!string.IsNullOrEmpty(integerPart) && long.Parse(integerPart) > 0)
        {
            int zeroCount = 0;
            for (int i = 0; i < integerPart.Length; i++)
            {
                int p = integerPart.Length - i - 1;
                int indexNumber = int.Parse(integerPart.Substring(i, 1));
                int quotient = p / 4;
                int modulus = p % 4;

                if (indexNumber == 0)
                {
                    zeroCount++;
                }
                else
                {
                    if (zeroCount > 0)
                    {
                        outputCharacters += chineseDigits[0];
                    }
                    zeroCount = 0;
                    outputCharacters += chineseDigits[indexNumber] + chineseSmallUnits[modulus];
                }

                if (modulus == 0 && zeroCount < 4)
                {
                    outputCharacters += chineseBigUnits[quotient];
                }
            }

            outputCharacters += CN_DOLLAR;
        }

        if (!string.IsNullOrEmpty(fractionPart))
        {
            for (int i = 0; i < fractionPart.Length; i++)
            {
                int indexNumber = int.Parse(fractionPart.Substring(i, 1));
                if (indexNumber != 0)
                {
                    outputCharacters += chineseDigits[indexNumber] + chineseDecimalsUnits[i];
                }
            }
        }

        if (outputCharacters == "")
        {
            outputCharacters = CN_ZERO + CN_DOLLAR;
        }

        if (string.IsNullOrEmpty(fractionPart))
        {
            outputCharacters += CN_INTEGER;
        }
        return outputCharacters;
    }

    /// <summary>
    /// 拆分金额
    /// </summary>
    /// <param name="value">金额</param>
    /// <param name="integerPart">整数</param>
    /// <param name="fractionPart">小数</param>
    /// <returns></returns>
    private static bool SplitMoney(decimal value, out string integerPart, out string fractionPart)
    {
        integerPart = string.Empty;
        fractionPart = string.Empty;
        try
        {
            string numberStr = value.ToString(CultureInfo.InvariantCulture);
            int decimalIndex = numberStr.IndexOf('.');
            if (decimalIndex > 0)
            {
                integerPart = numberStr.Substring(0, decimalIndex);
                fractionPart = numberStr.Substring(decimalIndex + 1);
            }
            else
            {
                integerPart = value.ToString();
            }
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值