.NET中是否有内置方法将数字转换为数字的字符串表示形式?例如,1变为1,2变为2,等等.
我一直是递归方法的粉丝
public static string NumberToText( int n) { if ( n < 0 ) return "Minus " + NumberToText(-n); else if ( n == 0 ) return ""; else if ( n <= 19 ) return new string[] {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}[n-1] + " "; else if ( n <= 99 ) return new string[] {"Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"}[n / 10 - 2] + " " + NumberToText(n % 10); else if ( n <= 199 ) return "One Hundred " + NumberToText(n % 100); else if ( n <= 999 ) return NumberToText(n / 100) + "Hundreds " + NumberToText(n % 100); else if ( n <= 1999 ) return "One Thousand " + NumberToText(n % 1000); else if ( n <= 999999 ) return NumberToText(n / 1000) + "Thousands " + NumberToText(n % 1000); else if ( n <= 1999999 ) return "One Million " + NumberToText(n % 1000000); else if ( n <= 999999999) return NumberToText(n / 1000000) + "Millions " + NumberToText(n % 1000000); else if ( n <= 1999999999 ) return "One Billion " + NumberToText(n % 1000000000); else return NumberToText(n / 1000000000) + "Billions " + NumberToText(n % 1000000000); }
资源
啊,可能没有一个类可以做到这一点,但有一个代码高尔夫问题,我提供了一个C#示例:
代码高尔夫:数字到单词
但是,它并不是最容易阅读的,它只能达到decimal.MaxValue,所以我写了一个新版本,它将尽可能高.
我找不到任何关于高于vigintillions的值的信息,但是如果你将值附加到th []数组中,你可以继续上升到你喜欢的范围.它仍然不支持分数,但我正在考虑在某些时候添加它.
static string NumericStringToWords(string NumericValue) { if ("0" == NumericValue) return "zero"; string[] units = { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; string[] teens = { "eleven", "twelve", "thirteen", "four", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" }; string[] tens = { "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" }; string[] thou = { "thousand", "million", "billion", "trillion", "quadrillion", "quintillion", "sextillion", "septillion", "octillion", "nonillion", "decillion", "udecillion", "duodecillion", "tredecillion", "quattuordecillion", "quindecillion", "sexdecillion", "septendecillion", "octodecillion", "novemdecillion", "vigintillion" }; string sign = String.Empty; if ("-" == NumericValue.Substring(0, 1)) { sign = "minus "; NumericValue = NumericValue.Substring(1); } int maxLen = thou.Length * 3; int actLen = NumericValue.Length; if(actLen > maxLen) throw new InvalidCastException(String.Format("{0} digit number specified exceeds the maximum length of {1} digits. To evaluate this number, you must first expand the thou[] array.", actLen, maxLen)); //Make sure that the value passed in is indeed numeric... we parse the entire string //rather than just cast to a numeric type to allow us to handle large number types passed //in as a string. Otherwise, we're limited to the standard data type sizes. int n; //We don't care about n, but int.TryParse requires it if (!NumericValue.All(c => int.TryParse(c.ToString(), out n))) throw new InvalidCastException(); string fraction = String.Empty; if (NumericValue.Contains(".")) { string[] split = NumericValue.Split('.'); NumericValue = split[0]; fraction = split[1]; } StringBuilder word = new StringBuilder(); ulong loopCount = 0; while (0 < NumericValue.Length) { int startPos = Math.Max(0, NumericValue.Length - 3); string crntBlock = NumericValue.Substring(startPos); if (0 < crntBlock.Length) { //Grab the hundreds tens & units for the current block int h = crntBlock.Length > 2 ? int.Parse(crntBlock[crntBlock.Length - 3].ToString()) : 0; int t = crntBlock.Length > 1 ? int.Parse(crntBlock[crntBlock.Length - 2].ToString()) : 0; int u = crntBlock.Length > 0 ? int.Parse(crntBlock[crntBlock.Length - 1].ToString()) : 0; StringBuilder thisBlock = new StringBuilder(); if (0 < u) thisBlock.Append(1 == t? teens[u - 1] : units[u - 1]); if (1 != t) { if (1 < t && 0 < u) thisBlock.Insert(0, "-"); if (0 < t) thisBlock.Insert(0, tens[t - 1]); } if (0 < h) { if (t > 0 | u > 0) thisBlock.Insert(0, " and "); thisBlock.Insert(0, String.Format("{0} hundred", units[h - 1])); } //Check to see if we've got any data left and add //appropriate word separator ("and" or ",") bool MoreLeft = 3 < NumericValue.Length; if (MoreLeft && (0 == h) && (0 == loopCount)) thisBlock.Insert(0, " and "); else if (MoreLeft) thisBlock.Insert(0, String.Format(" {0}, ", thou[loopCount])); word.Insert(0, thisBlock); } //Remove the block we just evaluated from the //input string for the next loop NumericValue = NumericValue.Substring(0, startPos); loopCount++; } return word.Insert(0, sign).ToString(); }
我使用附加到自身的Decimal.MaxValue来测试它以生成大量的:
7个octodecillion,922 septendecillion,816 sexdecillion,251 quindecillion,426 quattuordecillion,433 tredecillion,759 duodecillion,三一百54 udecillion,395 decillion,33 nonillion,579千的九次方,228 septillion,162 sextillion,五零四百万的三次方,二亿四千四百万亿,二亿三千七百万亿,五十九十三亿,五亿四千三百万,九十五万,三百三十五
public string IntToString(int number)//nobody really uses negative numbers { if(number == 0) return "zero"; else if(number == 1) return "one"; ....... else if(number == 2147483647) return "two billion one hundred forty seven million four hundred eighty three thousand six hundred forty seven"; }
这是我使用的修改代码:
//Wrapper class for NumberToText(int n) to account for single zero parameter. public static string ConvertToStringRepresentation(long number) { string result = null; if (number == 0) { result = "Zero"; } else { result = NumberToText(number); } return result; } //Found at http://www.dotnet2themax.com/blogs/fbalena/PermaLink,guid,cdceca73-08cd-4c15-aef7-0f9c8096e20a.aspx. //Modifications from original source: // Changed parameter type from int to long. // Changed labels to be singulars instead of plurals (Billions to Billion, Millions to Million, etc.). private static string NumberToText(long n) { if (n < 0) return "Minus " + NumberToText(-n); else if (n == 0) return ""; else if (n <= 19) return new string[] {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}[n - 1] + " "; else if (n <= 99) return new string[] {"Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"}[n / 10 - 2] + " " + NumberToText(n % 10); else if (n <= 199) return "One Hundred " + NumberToText(n % 100); else if (n <= 999) return NumberToText(n / 100) + "Hundred " + NumberToText(n % 100); else if (n <= 1999) return "One Thousand " + NumberToText(n % 1000); else if (n <= 999999) return NumberToText(n / 1000) + "Thousand " + NumberToText(n % 1000); else if (n <= 1999999) return "One Million " + NumberToText(n % 1000000); else if (n <= 999999999) return NumberToText(n / 1000000) + "Million " + NumberToText(n % 1000000); else if (n <= 1999999999) return "One Billion " + NumberToText(n % 1000000000); else return NumberToText(n / 1000000000) + "Billion " + NumberToText(n % 1000000000); }
这个帖子很有帮助.我非常喜欢Ryan Emerle的解决方案.这是我的版本,我认为结构清晰如日:
public static class Number { static string[] first = { "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" }; static string[] tens = { "Twenty", "Thirty", "Fourty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety", }; ////// Converts the given number to an english sentence. /// /// The number to convert. ///The string representation of the number. public static string ToSentence(int n) { return n == 0 ? first[n] : Step(n); } // traverse the number recursively public static string Step(int n) { return n < 0 ? "Minus " + Step(-n): n == 0 ? "": n <= 19 ? first[n]: n <= 99 ? tens[n / 10 - 2] + " " + Step(n % 10): n <= 199 ? "One Hundred " + Step(n % 100): n <= 999 ? Step(n / 100) + "Hundred " + Step(n % 100): n <= 1999 ? "One Thousand " + Step(n % 1000): n <= 999999 ? Step(n / 1000) + "Thousand " + Step(n % 1000): n <= 1999999 ? "One Million " + Step(n % 1000000): n <= 999999999 ? Step(n / 1000000) + "Million " + Step(n % 1000000): n <= 1999999999 ? "One Billion " + Step(n % 1000000000): Step(n / 1000000000) + "Billion " + Step(n % 1000000000); } }