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

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

相关推荐
sdgsdgdsgc1 小时前
Next.js企业级应用开发:SSR、ISR与性能监控方案
开发语言·前端·javascript
哲此一生9841 小时前
搭建Vue3工程(去除不必要的文件)
前端·javascript·vue.js
黑云压城After4 小时前
H5使用环信实现视频或语音通话
前端·javascript·vue.js
未来之窗软件服务5 小时前
自己写算法(九)网页数字动画函数——东方仙盟化神期
前端·javascript·算法·仙盟创梦ide·东方仙盟·东方仙盟算法
CsharpDev-奶豆哥6 小时前
ASP.NET中for和foreach使用指南
windows·microsoft·c#·asp.net·.net
你的人类朋友6 小时前
什么是断言?
前端·后端·安全
FIN66687 小时前
昂瑞微:实现精准突破,攻坚射频“卡脖子”难题
前端·人工智能·安全·前端框架·信息与通信
椎4957 小时前
苍穹外卖前端nginx错误之一解决
运维·前端·nginx
@。1247 小时前
对于灰度发布(金丝雀发布)的了解
开发语言·前端
我有一棵树7 小时前
前端图片加载失败、 img 出现裂图的原因全解析
前端