c#将int转为中文数字

csharp 复制代码
public static string IntegerToCN(int value)
        {
            string[] numberStrs = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十" };
            if (value <= 10)
            {
                value = Math.Max(0, value);
                return numberStrs[value];
            }
            string[] unitStrs = { "十", "百", "千", "", "十", "百", "千", "" };
            string unitWan = "万", unitYi = "亿";
            StringBuilder stringBuilder = new StringBuilder();
            int unitIndex = unitStrs.Length - 1;
            int unitInteger = 1_0000_0000;
            bool addZero = false;

            while (unitInteger > 0)
            {
                int value1 = value / unitInteger;
                if (value1 > 0)
                {
                    if (addZero)
                    {
                        stringBuilder.Append(numberStrs[0]);
                        addZero = false;
                    }

                    stringBuilder.Append(numberStrs[value1]);
                    if (unitIndex >= 0)
                    {
                        stringBuilder.Append(unitStrs[unitIndex]);
                    }
                    var newValue = value - value1 * unitInteger;
                    if (newValue < unitInteger / 10)
                    {
                        addZero = true;
                    }
                    if (value >= 1_0000_0000 && newValue < 1_0000_0000)
                    {
                        stringBuilder.Append(unitYi);
                    }
                    else if (value >= 1_0000 && newValue < 1_0000)
                    {
                        stringBuilder.Append(unitWan);
                    }
                    value = newValue;
                }
                unitIndex--;
                unitInteger = unitInteger / 10;
            }
            return stringBuilder.ToString();
        }
csharp 复制代码
            while (true)
            {
                var inputStr = Console.ReadLine();
                Console.WriteLine(IntegerToCN(int.Parse(inputStr)));
            }

测试: 输入: 123456789

输出: 一亿二千三百四十五万六千七百八十九

相关推荐
xiaopengbc5 分钟前
在 React 中如何使用 useMemo 和 useCallback 优化性能?
前端·javascript·react.js
GISer_Jing7 分钟前
React 18 过渡更新:并发渲染的艺术
前端·javascript·react.js
全栈技术负责人38 分钟前
前端网络性能优化实践:从 HTTP 请求到 HTTPS 与 HTTP/2 升级
前端·网络·http
文弱书生65639 分钟前
5.后台运行设置和包设计与实现
服务器·开发语言·c#
码上暴富1 小时前
Echarts雷达图根据数值确定颜色
前端·javascript·echarts
Mintopia1 小时前
在混沌宇宙中捕捉错误的光——Next.js 全栈 Sentry / LogRocket
前端·javascript·next.js
Mintopia1 小时前
长文本 AIGC:Web 端大篇幅内容生成的技术优化策略
前端·javascript·aigc
VueVirtuoso1 小时前
SaaS 建站从 0 到 1 教程:Vue 动态域名 + 后端子域名管理 + Nginx 配置
前端·vue.js·nginx
少年阿闯~~1 小时前
transition(过渡)和animation(动画)——CSS
前端·css·动画·过渡
Async Cipher1 小时前
CSS 继承 (Inheritance)
前端·css