Nth Number To Text - CSharp System

CSharp examples for System:String Number

Description

Nth Number To Text

Demo Code


using System.Text.RegularExpressions;
using System.Text;
using System.Globalization;
using System.Collections.Generic;
using System;/*from  w  w  w.j a  va 2s .  co m*/

public class Main{
        public static string NthNumberToText(int n)
        {                       
                if (n < 0)
                    return "Minus " + NthNumberToText(-n);
                else if (n == 0)
                    return "";
                else if (n <= 19)
                    return new string[] {"First", "Second", "Third", "Fourth", "Fifth", "Sixth", "Seventh", "Eighth", 
         "Nineth", "Tenth", "Eleventh", "Twelfth", "Thirteenth", "Fourteenth", "Fifteenth", "Sixteenth", 
         "Seventeenth", "Eighteenth", "Nineteenth"}[n - 1] + " ";
                else if (n <= 99)
                    return new string[] {"Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", 
         "Eighty", "Ninety"}[n / 10 - 2] + " " + NthNumberToText(n % 10);
                else if (n <= 199)
                    return "One Hundred " + NthNumberToText(n % 100);
                else if (n <= 999)
                    return NthNumberToText(n / 100) + "Hundred " + NthNumberToText(n % 100);
                else if (n <= 1999)
                    return "One Thousand " + NthNumberToText(n % 1000);
                else if (n <= 999999)
                    return NthNumberToText(n / 1000) + "Thousand " + NthNumberToText(n % 1000);
                else if (n <= 1999999)
                    return "One Million " + NthNumberToText(n % 1000000);
                else if (n <= 999999999)
                    return NthNumberToText(n / 1000000) + "Million " + NthNumberToText(n % 1000000);
                else if (n <= 1999999999)
                    return "One Billion " + NthNumberToText(n % 1000000000);
                else
                    return NthNumberToText(n / 1000000000) + "Billion " + NthNumberToText(n % 1000000000);    
        }
}

Related Tutorials