andrew4582

Spell Numeric

Aug 15th, 2010
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.36 KB | None | 0 0
  1.  
  2. public class SpellNumeric {
  3. public static string IntegerToWords(long inputNum) {
  4. long dig1,dig2,dig3,level = 0,lasttwo,threeDigits;
  5. string retval = "";
  6. string x = "";
  7. string[] ones ={
  8. " ",
  9. "one",
  10. "two",
  11. "three",
  12. "four",
  13. "five",
  14. "six",
  15. "seven",
  16. "eight",
  17. "nine",
  18. "ten",
  19. "eleven",
  20. "twelve",
  21. "thirteen",
  22. "fourteen",
  23. "fifteen",
  24. "sixteen",
  25. "seventeen",
  26. "eighteen",
  27. "nineteen"
  28.   };
  29. string[] tens ={
  30. " ",
  31. "ten",
  32. "twenty",
  33. "thirty",
  34. "forty",
  35. "fifty",
  36. "sixty",
  37. "seventy",
  38. "eighty",
  39. "ninety"
  40.   };
  41. string[] thou ={
  42. "",
  43. "thousand",
  44. "million",
  45. "billion",
  46. "trillion",
  47. "quadrillion",
  48. "quintillion"
  49.   };
  50. bool isNegative = false;
  51. if(inputNum < 0) {
  52. isNegative = true;
  53. inputNum *= -1;
  54. }
  55. if(inputNum == 0)
  56. return ("zero");
  57. string s = inputNum.ToString();
  58. while(s.Length > 0) {
  59. // Get the three rightmost characters
  60. x = (s.Length < 3) ? s : s.Substring(s.Length - 3,3);
  61. // Separate the three digits
  62. threeDigits = long.Parse(x);
  63. lasttwo = threeDigits % 100;
  64. dig1 = threeDigits / 100;
  65. dig2 = lasttwo / 10;
  66. dig3 = (threeDigits % 10);
  67. // append a "thousand" where appropriate
  68. if(level > 0 && dig1 + dig2 + dig3 > 0) {
  69. retval = thou[level] + " " + retval;
  70. retval = retval.Trim();
  71. }
  72. // check that the last two digits is not a zero
  73. if(lasttwo > 0) {
  74. if(lasttwo < 20) // if less than 20, use "ones" only
  75. retval = ones[lasttwo] + " " + retval;
  76. else // otherwise, use both "tens" and "ones" array
  77. retval = tens[dig2] + " " + ones[dig3] + " " + retval;
  78. }
  79. // if a hundreds part is there, translate it
  80. if(dig1 > 0)
  81. retval = ones[dig1] + " hundred " + retval;
  82. s = (s.Length - 3) > 0 ? s.Substring(0,s.Length - 3) : "";
  83. level++;
  84. }
  85. while(retval.IndexOf("  ") > 0)
  86. retval = retval.Replace("  "," ");
  87. retval = retval.Trim();
  88. if(isNegative)
  89. retval = "negative " + retval;
  90. return (retval);
  91. }
  92. public static string SpellDecimal(decimal number) {
  93. string[] digit =
  94. {
  95. "", "one", "two", "three", "four", "five", "six",
  96. "seven", "eight", "nine", "ten", "eleven", "twelve",
  97. "thirteen", "fourteen", "fifteen", "sixteen",
  98. "seventeen", "eighteen", "nineteen"
  99. };
  100. string[] baseten =
  101. {
  102. "", "", "twenty", "thirty", "fourty", "fifty",
  103. "sixty", "seventy", "eighty", "ninety"
  104. };
  105. string[] expo =
  106. {
  107. "", "thousand", "million", "billion", "trillion",
  108. "quadrillion", "quintillion"
  109. };
  110. StringBuilder sb = new StringBuilder();
  111. int thousands = 0;
  112. decimal power = 1;
  113. if(number < 0) {
  114. sb.Append("minus ");
  115. number = -number;
  116. }
  117. decimal n = Decimal.Truncate(number);
  118. decimal cents = Decimal.Truncate((number - n) * 100);
  119. if(n == Decimal.Zero)
  120. sb.Append("zero");
  121. for(decimal i = n;i >= 1000;i /= 1000) {
  122. power *= 1000;
  123. thousands++;
  124. }
  125. bool sep = false;
  126. for(decimal i = n;thousands >= 0;i %= power,thousands--,power /= 1000) {
  127. int j = (int)(i / power);
  128. int k = j % 100;
  129. int hundreds = j / 100;
  130. int tens = j % 100 / 10;
  131. int ones = j % 10;
  132. if(j == 0)
  133. continue;
  134. if(hundreds > 0) {
  135. if(sep)
  136. sb.Append(", ");
  137. sb.Append(digit[hundreds]);
  138. sb.Append(" hundred");
  139. sep = true;
  140. }
  141. if(k != 0) {
  142. if(sep) {
  143. sb.Append(" and ");
  144. sep = false;
  145. }
  146. if(k < 20)
  147. sb.Append(digit[k]);
  148. else {
  149. sb.Append(baseten[tens]);
  150. if(ones > 0) {
  151. sb.Append("-");
  152. sb.Append(digit[ones]);
  153. }
  154. }
  155. }
  156. if(thousands > 0) {
  157. sb.Append(" ");
  158. sb.Append(expo[thousands]);
  159. sep = true;
  160. }
  161. }
  162. sb.Append(" and ");
  163. if(cents < 10)
  164. sb.Append("0");
  165. sb.Append(cents);
  166. sb.Append("/100");
  167. return sb.ToString();
  168. }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment