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

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

相关推荐
wangfpp1 分钟前
多端统一你真的会了吗?
前端·javascript·架构
小霍同学1 分钟前
Vue 动态组件(Dynamic Components)
前端·vue.js
代码煮茶9 分钟前
Vue3 组件封装实战 | 从 0 封装一个可复用的表格组件(附插槽 / Props 设计)
前端·vue.js
兜兜风11 分钟前
从零部署 OpenClaw:打造你的第二大脑
前端·后端
Maimai1080811 分钟前
Next.js 16 缓存策略详解:从旧模型到 Cache Components
开发语言·前端·javascript·react.js·缓存·前端框架·reactjs
凌览12 分钟前
OpenClaw创始人炮轰腾讯"只抄不养",腾讯喊冤
前端·后端
jwn99912 分钟前
【JavaEE】Spring Web MVC
前端·spring·java-ee
下北沢美食家13 分钟前
React面试题
前端·javascript·react.js
小曹要微笑19 分钟前
C#的运算符重载
开发语言·c#·运算符重载·c#运算符重载
我是唐青枫21 分钟前
C#.NET Channel 深入解析:高性能异步生产者消费者模型实战
开发语言·c#·.net