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

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

相关推荐
程序员大金37 分钟前
基于SpringBoot+Vue+MySQL的特色旅游网站系统
java·前端·vue.js·spring boot·后端·mysql·tomcat
游仙好梦40 分钟前
Vitepress 自定义主题开发教程
前端·开源·vitepress
wiseyao12191 小时前
C#中判断socket是否已断开的方法
c#
friklogff1 小时前
【C#生态园】从容面对.NET性能挑战:全面解析多种性能监控工具
开发语言·c#·.net
getaxiosluo1 小时前
详解Vite创建Vue3项目router-less-scss-pinia-持久化
前端·vue.js·chrome·typescript·less·scss
用你的胜利博我一笑吧1 小时前
supermap iclient3d for cesium中entity使用
前端·javascript·vue.js·3d·cesium·supermap
初心魏2 小时前
Uniapp 跨域
前端·数据库·uni-app
E-iceblue2 小时前
C# 裁剪PDF页面
c#·pdf api·pdf页面·裁剪pdf
学会沉淀。2 小时前
Vue3快速入门+axios的异步请求(基础使用)
前端·javascript·vue.js
雷工笔记2 小时前
C#知识|软件接口的认识
c#