Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class SpellNumeric {
- public static string IntegerToWords(long inputNum) {
- long dig1,dig2,dig3,level = 0,lasttwo,threeDigits;
- string retval = "";
- string x = "";
- string[] ones ={
- " ",
- "one",
- "two",
- "three",
- "four",
- "five",
- "six",
- "seven",
- "eight",
- "nine",
- "ten",
- "eleven",
- "twelve",
- "thirteen",
- "fourteen",
- "fifteen",
- "sixteen",
- "seventeen",
- "eighteen",
- "nineteen"
- };
- string[] tens ={
- " ",
- "ten",
- "twenty",
- "thirty",
- "forty",
- "fifty",
- "sixty",
- "seventy",
- "eighty",
- "ninety"
- };
- string[] thou ={
- "",
- "thousand",
- "million",
- "billion",
- "trillion",
- "quadrillion",
- "quintillion"
- };
- bool isNegative = false;
- if(inputNum < 0) {
- isNegative = true;
- inputNum *= -1;
- }
- if(inputNum == 0)
- return ("zero");
- string s = inputNum.ToString();
- while(s.Length > 0) {
- // Get the three rightmost characters
- x = (s.Length < 3) ? s : s.Substring(s.Length - 3,3);
- // Separate the three digits
- threeDigits = long.Parse(x);
- lasttwo = threeDigits % 100;
- dig1 = threeDigits / 100;
- dig2 = lasttwo / 10;
- dig3 = (threeDigits % 10);
- // append a "thousand" where appropriate
- if(level > 0 && dig1 + dig2 + dig3 > 0) {
- retval = thou[level] + " " + retval;
- retval = retval.Trim();
- }
- // check that the last two digits is not a zero
- if(lasttwo > 0) {
- if(lasttwo < 20) // if less than 20, use "ones" only
- retval = ones[lasttwo] + " " + retval;
- else // otherwise, use both "tens" and "ones" array
- retval = tens[dig2] + " " + ones[dig3] + " " + retval;
- }
- // if a hundreds part is there, translate it
- if(dig1 > 0)
- retval = ones[dig1] + " hundred " + retval;
- s = (s.Length - 3) > 0 ? s.Substring(0,s.Length - 3) : "";
- level++;
- }
- while(retval.IndexOf(" ") > 0)
- retval = retval.Replace(" "," ");
- retval = retval.Trim();
- if(isNegative)
- retval = "negative " + retval;
- return (retval);
- }
- public static string SpellDecimal(decimal number) {
- string[] digit =
- {
- "", "one", "two", "three", "four", "five", "six",
- "seven", "eight", "nine", "ten", "eleven", "twelve",
- "thirteen", "fourteen", "fifteen", "sixteen",
- "seventeen", "eighteen", "nineteen"
- };
- string[] baseten =
- {
- "", "", "twenty", "thirty", "fourty", "fifty",
- "sixty", "seventy", "eighty", "ninety"
- };
- string[] expo =
- {
- "", "thousand", "million", "billion", "trillion",
- "quadrillion", "quintillion"
- };
- StringBuilder sb = new StringBuilder();
- int thousands = 0;
- decimal power = 1;
- if(number < 0) {
- sb.Append("minus ");
- number = -number;
- }
- decimal n = Decimal.Truncate(number);
- decimal cents = Decimal.Truncate((number - n) * 100);
- if(n == Decimal.Zero)
- sb.Append("zero");
- for(decimal i = n;i >= 1000;i /= 1000) {
- power *= 1000;
- thousands++;
- }
- bool sep = false;
- for(decimal i = n;thousands >= 0;i %= power,thousands--,power /= 1000) {
- int j = (int)(i / power);
- int k = j % 100;
- int hundreds = j / 100;
- int tens = j % 100 / 10;
- int ones = j % 10;
- if(j == 0)
- continue;
- if(hundreds > 0) {
- if(sep)
- sb.Append(", ");
- sb.Append(digit[hundreds]);
- sb.Append(" hundred");
- sep = true;
- }
- if(k != 0) {
- if(sep) {
- sb.Append(" and ");
- sep = false;
- }
- if(k < 20)
- sb.Append(digit[k]);
- else {
- sb.Append(baseten[tens]);
- if(ones > 0) {
- sb.Append("-");
- sb.Append(digit[ones]);
- }
- }
- }
- if(thousands > 0) {
- sb.Append(" ");
- sb.Append(expo[thousands]);
- sep = true;
- }
- }
- sb.Append(" and ");
- if(cents < 10)
- sb.Append("0");
- sb.Append(cents);
- sb.Append("/100");
- return sb.ToString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment