Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm not sure if this is exactly how you say it, but I'll give it a try:</p> <pre><code>//this array specifies the names of the powers of 10 static Tuple&lt;int, string&gt;[] powers = { new Tuple&lt;int, string&gt;(0, ""), new Tuple&lt;int, string&gt;(3, "thousand"), new Tuple&lt;int, string&gt;(5, "lac"), new Tuple&lt;int, string&gt;(7, "crore") }; //this array specifies the digits' names static string[] digits = { "", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; static string[] extendedDigits = { "ten", "eleven", "twelve", "thirteen", "fourteen", "fivteen", "sixteen", "seventeen", "eighteen", "nineteen" }; static string[] tensWords = { "", "", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety" }; private static string NumberToWords(long number) { var sb = new StringBuilder(); //begin with the left most digit (greatest power of 10) for (int i = powers.Length - 1; i &gt;= 0; --i) { //translate the current part only (for a known power of 10) //usually part is a 3-digit number int part = (int)(number / (long)Math.Pow(10, powers[i].Item1)); //if the part is 0, we don't have to add anything if (part &gt; 0) { //extract the hundreds int hundreds = part / 100; if(hundreds &gt; 9) throw new ArgumentException(number + " is too large and cannot be expressed."); if (hundreds &gt; 0) { //if there are hundreds, copy them to the output sb.Append(digits[hundreds]); sb.Append(" hundred "); } //convert the next two digits sb.Append(TwoDigitNumberToWord(part % 100)); sb.Append(" "); //and append the name of the power of 10 sb.Append(powers[i].Item2); sb.Append(" "); //subtract the currently managed part number -= part * (long)Math.Pow(10, powers[i].Item1); } } return sb.ToString(); } private static string TwoDigitNumberToWord(int number) { //one digit case if (number &lt; 10) return digits[number]; //special case 10 &lt;= n &lt;= 19 if (number &lt; 20) return extendedDigits[number - 10]; int tens = number / 10; int ones = number % 10; //concatenate the word from the two digits' words return tensWords[tens] + " " + digits[ones]; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload