偷懒一下下

需要把一个C#的函数改写成js的函数。于是俺就打开百度,点击AI。用下面的提示词,使用AI生成了一个js函数,复制到页面上,执行正常。

你是一个js程序员,请把下面这段C#的函数 改写成js的函数。

 public static string NumberToChn(int i_number)
        {
            string[] UNITS = { "", "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千" };
            string[] NUMS = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
            if (i_number == 0)
            {
                return NUMS[0];
            }
            string results = "";
            for (int i = i_number.ToString().Length - 1; i >= 0; i--)
            {
                int r = (int)(i_number / (Math.Pow(10, i)));
                results += NUMS[r % 10] + UNITS[i];
            }
            results = results.Replace("零十", "零")
                             .Replace("零百", "零")
                             .Replace("零千", "零")
                             .Replace("亿万", "亿");
            results = Regex.Replace(results, "零([万, 亿])", "$1");
            results = Regex.Replace(results, "零+", "零");

            if (results.StartsWith("一十"))
            {
                results = results.Substring(1);
            }
            cutzero:
            if (results.EndsWith("零"))
            {
                results = results.Substring(0, results.Length - 1);
                if (results.EndsWith("零"))
                {
                    goto cutzero;
                }
            }
            return results;
        }


function numberToChn(i_number) {
    const UNITS = ["", "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千"];
    const NUMS = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
    
    if (i_number === 0) {
        return NUMS[0];
    }
    
    let results = "";
    const numStr = i_number.toString();
    
    for (let i = numStr.length - 1; i >= 0; i--) {
        const r = Math.floor(i_number / Math.pow(10, i));
        results += NUMS[r % 10] + UNITS[i];
    }
    
    results = results.replace(/零十/g, "零")
                    .replace(/零百/g, "零")
                    .replace(/零千/g, "零")
                    .replace(/亿万/g, "亿")
                    .replace(/零([万亿])/g, "$1")
                    .replace(/零+/g, "零");
    
    if (results.startsWith("一十")) {
        results = results.substring(1);
    }
    
    while (results.endsWith("零")) {
        results = results.substring(0, results.length - 1);
    }
    
    return results;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

月巴月巴白勺合鸟月半

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

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

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

打赏作者

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

抵扣说明:

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

余额充值