您的位置:首页 > 科技 > 能源 > c#将int转为中文数字

c#将int转为中文数字

2024/12/23 8:42:58 来源:https://blog.csdn.net/qq_28002559/article/details/142136237  浏览:    关键词:c#将int转为中文数字
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();}
            while (true){var inputStr = Console.ReadLine();Console.WriteLine(IntegerToCN(int.Parse(inputStr)));}

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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com