C#数字转文字:如何区分0.01的小数1与整数1的显示值
解决方案
要实现整数部分和小数部分数字的不同文字映射,核心是让数字转换逻辑能区分当前处理的是整数还是小数位,从而使用不同的词汇表。以下是修改后的代码及关键说明:
关键修改点
- 新增小数位专用序数词数组,对应小数部分的数字(比如1对应"първа")
- 给
Convert方法添加参数,标记当前处理的是整数还是小数 - 在
ConvertAmount中调用Convert时,分别传入对应标记
修改后的完整代码
using System; class Program { static void Main(string[] args) { Console.OutputEncoding = System.Text.Encoding.Unicode; try { Console.WriteLine("Въведете число.....:"); string numberInput = Console.ReadLine(); string result = ConvertAmount(double.Parse(numberInput)); Console.WriteLine("Числото изписано в думи е : \n{0}", result); Console.ReadKey(); } catch (Exception ex) { Console.WriteLine(ex.Message); } } // 整数部分基础词汇 private static string[] units = { "нула", "едно", "две", "три", "четири", "пет", "шест", "седем", "осем", "девет", "десет", "единайсет", "дванайсет", "тринайсет", "четиринайсет", "петнайсет", "шеснайсет", "седемнайсет", "осемнайсет", "деветнайсет" }; private static string[] tens = { "", "", "двайсет", "трийсет", "четиресет", "педесет", "шейсет", "седемдесет", "осемдесет", "деведесет" }; private static string[] hundreds = {"","сто", "двеста", "триста","четиристотин", "петстотин", "шестстотин", "седемстотин", "осемстотин", "деветстотин" }; private static string[] thousands = {"","хиляда","две хиялди", "три хиляди","четири хиляди", "пет хиляди", "шест хиляди", "седем хиляди", "осем хиляди", "девет хиляди" }; // 小数部分序数词汇(对应分位的表述) private static string[] unitsDecimal = { "нула", "първа", "втора", "трета", "четвърта", "пета", "шеста", "седма", "осма", "девета", "десета", "единайсета", "дванайсета", "тринайсета", "четиринайсета", "петнайсета", "шеснайсета", "седемнайсета", "осемнайсета", "деветнайсета" }; public static string ConvertAmount(double amount) { try { double mn = 0.09; long amount_int = (long)amount; long amount_dec = (long)Math.Round((decimal)(amount - (double)amount_int) * 100); if (amount_dec == 0) { return Convert(amount_int, isDecimal: false) + " лв."; } else if (amount_int <= mn) { return Convert(amount_int, isDecimal: false) + " Цяло и Нула " + Convert(amount_dec, isDecimal: true) + " ст."; } else { return Convert(amount_int, isDecimal: false) + " лв. и " + Convert(amount_dec, isDecimal: true) + " ст."; } } catch (Exception e) { Console.WriteLine(e.Message); return ""; } } // 添加isDecimal参数,区分整数/小数处理逻辑 public static string Convert(long i, bool isDecimal = false) { Console.OutputEncoding = System.Text.Encoding.Unicode; // 根据是否为小数选择对应词汇表 string[] targetUnits = isDecimal ? unitsDecimal : units; if (i <= 20) { return targetUnits[i]; } if (i < 100) { string tenPart = tens[i / 10]; string unitPart = i % 10 > 0 ? " и " + Convert(i % 10, isDecimal) : ""; return tenPart + unitPart; } if (i < 1000) { string hundredPart = hundreds[i / 100]; string restPart = i % 100 > 0 ? " и " + Convert(i % 100, isDecimal) : ""; return hundredPart + restPart; } if (i < 100000) { string thousandPart = Convert(i / 1000, isDecimal) + " Хиляди "; string restPart = i % 1000 > 0 ? " " + Convert(i % 1000, isDecimal) : ""; return thousandPart + restPart; } else if (i < 1000000) { string tenThousandPart = Convert(i / 10000, isDecimal) + " Хиляди "; string restPart = i % 10000 > 0 ? " и " + Convert(i % 10000, isDecimal) : ""; return tenThousandPart + restPart; } if (i < 10000000) { string millionPart = Convert(i / 1000000, isDecimal) + " Милиона "; string restPart = i % 1000000 > 0 ? " " + Convert(i % 1000000, isDecimal) : ""; return millionPart + restPart; } if (i < 100000000) { string tenMillionPart = Convert(i / 1000000, isDecimal) + " Милиона "; string restPart = i % 1000000 > 0 ? " и " + Convert(i % 1000000, isDecimal) : ""; return tenMillionPart + restPart; } if (i < 1000000000) { string hundredMillionPart = Convert(i / 1000000, isDecimal) + " Милиона "; string restPart = i % 1000000 > 0 ? " " + Convert(i % 1000000, isDecimal) : ""; return hundredMillionPart + restPart; } if (i < 10000000000) { string billionPart = Convert(i / 1000000000, isDecimal) + " Милиа рда "; string restPart = i % 1000000000 > 0 ? " " + Convert(i % 1000000000, isDecimal) : ""; return billionPart + restPart; } string trillionPart = Convert(i / 1000000000000, isDecimal) + " "; string restPart = i % 1000000000000 > 0 ? " " + Convert(i % 1000000000000, isDecimal) : ""; return trillionPart + restPart; } }
测试验证
- 输入
0.01,输出:нула Цяло и Нула първа ст.(符合需求) - 输入
1,输出:едно лв.(整数1保持原表述) - 输入
1.02,输出:едно лв. и втора ст.(小数2对应序数词)
内容的提问来源于stack exchange,提问作者Space Dogge
相关产品推荐
相关产品推荐

