是否有一个库或类/函数可用于将整数转换为它的口头表示?
输入示例:
4,567,788`
示例输出:
四百万,五十七万七千七百八十八
i3arnon.. 47
目前,最好,最强大的库肯定是Humanizer.它是开源的,可用作nuget:
Console.WriteLine(4567788.ToWords()); // => four million five hundred and sixty-seven thousand seven hundred and eighty-eight
它还有各种各样的工具,可以解决每个应用程序对string
s,enum
s,DateTime
s,TimeSpan
s等的小问题,并支持许多不同的语言.
Console.WriteLine(4567788.ToOrdinalWords().Underscore().Hyphenate().ApplyCase(LetterCasing.AllCaps)); // => FOUR-MILLION-FIVE-HUNDRED-AND-SIXTY-SEVEN-THOUSAND-SEVEN-HUNDRED-AND-EIGHTY-EIGHTH
低估的答案.这是问题的正确答案. (4认同)
哇,这是多种语言:南非荷兰语,阿拉伯语,孟加拉语,巴西葡萄牙语,荷兰语,英语,波斯语,芬兰语,法语,德语,希伯来语,意大利语,挪威语,波兰语,罗马尼亚语,俄语,塞尔维亚语,塞尔维亚语,斯洛文尼亚语,西班牙语,乌克兰语.请参阅https://github.com/Humanizr/Humanizer/tree/dev/src/Humanizer/Localisation/NumberToWords (2认同)
小智.. 28
如果你使用以下代码: 将数字转换为单词C# ,你需要它为十进制数字,这里是如何做到这一点:
public string DecimalToWords(decimal number) { if (number == 0) return "zero"; if (number < 0) return "minus " + DecimalToWords(Math.Abs(number)); string words = ""; int intPortion = (int)number; decimal fraction = (number - intPortion)*100; int decPortion = (int)fraction; words = NumericToWords(intPortion); if (decPortion > 0) { words += " and "; words += NumericToWords(decPortion); } return words; }
Hannele.. 9
完全递归版本:
private static string[] ones = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", }; private static string[] tens = { "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" }; private static string[] thous = { "hundred", "thousand", "million", "billion", "trillion", "quadrillion" }; private static string fmt_negative = "negative {0}"; private static string fmt_dollars_and_cents = "{0} dollars and {1} cents"; private static string fmt_tens_ones = "{0}-{1}"; // e.g. for twenty-one, thirty-two etc. You might want to use an en-dash or em-dash instead of a hyphen. private static string fmt_large_small = "{0} {1}"; // stitches together the large and small part of a number, like "{three thousand} {five hundred forty two}" private static string fmt_amount_scale = "{0} {1}"; // adds the scale to the number, e.g. "{three} {million}"; public static string ToWords(decimal number) { if (number < 0) return string.format(fmt_negative, ToWords(Math.Abs(number))); int intPortion = (int)number; int decPortion = (int)((number - intPortion) * (decimal) 100); return string.Format(fmt_dollars_and_cents, ToWords(intPortion), ToWords(decPortion)); } private static string ToWords(int number, string appendScale = "") { string numString = ""; // if the number is less than one hundred, then we're mostly just pulling out constants from the ones and tens dictionaries if (number < 100) { if (number < 20) numString = ones[number]; else { numString = tens[number / 10]; if ((number % 10) > 0) numString = string.Format(fmt_tens_ones, numString, ones[number % 10]); } } else { int pow = 0; // we'll divide the number by pow to figure out the next chunk string powStr = ""; // powStr will be the scale that we append to the string e.g. "hundred", "thousand", etc. if (number < 1000) { // number is between 100 and 1000 pow = 100; // so we'll be dividing by one hundred powStr = thous[0]; // and appending the string "hundred" } else { // find the scale of the number // log will be 1, 2, 3 for 1_000, 1_000_000, 1_000_000_000, etc. int log = (int)Math.Log(number, 1000); // pow will be 1_000, 1_000_000, 1_000_000_000 etc. pow = (int)Math.Pow(1000, log); // powStr will be thousand, million, billion etc. powStr = thous[log]; } // we take the quotient and the remainder after dividing by pow, and call ToWords on each to handle cases like "{five thousand} {thirty two}" (curly brackets added for emphasis) numString = string.Format(fmt_large_small, ToWords(number / pow, powStr), ToWords(number % pow)).Trim(); } // and after all of this, if we were passed in a scale from above, we append it to the current number "{five} {thousand}" return string.Format(fmt_amount_scale, numString, appendScale).Trim(); }
电流可以达到(短程)四度.只需更改变量即可添加额外的支持(对于较大的数字,或对于长规模)thous
.
考虑到修改非递归版本也相当简单,也许,不必要的复杂(特殊情况下数百个错误).
目前,最好,最强大的库肯定是Humanizer.它是开源的,可用作nuget:
Console.WriteLine(4567788.ToWords()); // => four million five hundred and sixty-seven thousand seven hundred and eighty-eight
它还有各种各样的工具,可以解决每个应用程序对string
s,enum
s,DateTime
s,TimeSpan
s等的小问题,并支持许多不同的语言.
Console.WriteLine(4567788.ToOrdinalWords().Underscore().Hyphenate().ApplyCase(LetterCasing.AllCaps)); // => FOUR-MILLION-FIVE-HUNDRED-AND-SIXTY-SEVEN-THOUSAND-SEVEN-HUNDRED-AND-EIGHTY-EIGHTH
如果你使用以下代码: 将数字转换为单词C# ,你需要它为十进制数字,这里是如何做到这一点:
public string DecimalToWords(decimal number) { if (number == 0) return "zero"; if (number < 0) return "minus " + DecimalToWords(Math.Abs(number)); string words = ""; int intPortion = (int)number; decimal fraction = (number - intPortion)*100; int decPortion = (int)fraction; words = NumericToWords(intPortion); if (decPortion > 0) { words += " and "; words += NumericToWords(decPortion); } return words; }
完全递归版本:
private static string[] ones = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", }; private static string[] tens = { "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" }; private static string[] thous = { "hundred", "thousand", "million", "billion", "trillion", "quadrillion" }; private static string fmt_negative = "negative {0}"; private static string fmt_dollars_and_cents = "{0} dollars and {1} cents"; private static string fmt_tens_ones = "{0}-{1}"; // e.g. for twenty-one, thirty-two etc. You might want to use an en-dash or em-dash instead of a hyphen. private static string fmt_large_small = "{0} {1}"; // stitches together the large and small part of a number, like "{three thousand} {five hundred forty two}" private static string fmt_amount_scale = "{0} {1}"; // adds the scale to the number, e.g. "{three} {million}"; public static string ToWords(decimal number) { if (number < 0) return string.format(fmt_negative, ToWords(Math.Abs(number))); int intPortion = (int)number; int decPortion = (int)((number - intPortion) * (decimal) 100); return string.Format(fmt_dollars_and_cents, ToWords(intPortion), ToWords(decPortion)); } private static string ToWords(int number, string appendScale = "") { string numString = ""; // if the number is less than one hundred, then we're mostly just pulling out constants from the ones and tens dictionaries if (number < 100) { if (number < 20) numString = ones[number]; else { numString = tens[number / 10]; if ((number % 10) > 0) numString = string.Format(fmt_tens_ones, numString, ones[number % 10]); } } else { int pow = 0; // we'll divide the number by pow to figure out the next chunk string powStr = ""; // powStr will be the scale that we append to the string e.g. "hundred", "thousand", etc. if (number < 1000) { // number is between 100 and 1000 pow = 100; // so we'll be dividing by one hundred powStr = thous[0]; // and appending the string "hundred" } else { // find the scale of the number // log will be 1, 2, 3 for 1_000, 1_000_000, 1_000_000_000, etc. int log = (int)Math.Log(number, 1000); // pow will be 1_000, 1_000_000, 1_000_000_000 etc. pow = (int)Math.Pow(1000, log); // powStr will be thousand, million, billion etc. powStr = thous[log]; } // we take the quotient and the remainder after dividing by pow, and call ToWords on each to handle cases like "{five thousand} {thirty two}" (curly brackets added for emphasis) numString = string.Format(fmt_large_small, ToWords(number / pow, powStr), ToWords(number % pow)).Trim(); } // and after all of this, if we were passed in a scale from above, we append it to the current number "{five} {thousand}" return string.Format(fmt_amount_scale, numString, appendScale).Trim(); }
电流可以达到(短程)四度.只需更改变量即可添加额外的支持(对于较大的数字,或对于长规模)thous
.
考虑到修改非递归版本也相当简单,也许,不必要的复杂(特殊情况下数百个错误).