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

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

相关推荐
落霞的思绪44 分钟前
CSS复习
前端·css
咖啡の猫3 小时前
Shell脚本-for循环应用案例
前端·chrome
百万蹄蹄向前冲5 小时前
Trae分析Phaser.js游戏《洋葱头捡星星》
前端·游戏开发·trae
朝阳5816 小时前
在浏览器端使用 xml2js 遇到的报错及解决方法
前端
GIS之路6 小时前
GeoTools 读取影像元数据
前端
ssshooter6 小时前
VSCode 自带的 TS 版本可能跟项目TS 版本不一样
前端·面试·typescript
Jerry7 小时前
Jetpack Compose 中的状态
前端
dae bal8 小时前
关于RSA和AES加密
前端·vue.js
柳杉8 小时前
使用three.js搭建3d隧道监测-2
前端·javascript·数据可视化
lynn8570_blog8 小时前
低端设备加载webp ANR
前端·算法