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

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

相关推荐
疯狂踩坑人31 分钟前
【React 19 尝鲜】第一篇:use和useActionState
前端·react.js
毕设源码-邱学长34 分钟前
【开题答辩全过程】以 基于VUE的打车系统的设计与实现为例,包含答辩的问题和答案
前端·javascript·vue.js
用户390513321928837 分钟前
JS判断空值只知道“||”?不如来试试这个操作符
前端·javascript
海云前端138 分钟前
前端面试必问 asyncawait 到底要不要加 trycatch 90% 人踩坑 求职加分技巧揭秘
前端
wuk9981 小时前
梁非线性动力学方程MATLAB编程实现
前端·javascript·matlab
XiaoYu20022 小时前
第11章 LangChain
前端·javascript·langchain
霉运全滚蛋好运围着转2 小时前
启动 Taro 4 项目报错:Error: The specified module could not be found.
前端
cxxcode2 小时前
前端模块化发展
前端
不务正业的前端学徒2 小时前
docker+nginx部署
前端
不务正业的前端学徒2 小时前
webpack/vite配置
前端