As the title suggests I would like to convert a long number to the format with words using C#. The Culture settings don't seem to do this and I am just currently doing this
String.Format(new CultureInfo("en-IN"), "{0:C0}", Price)
But for very long numbers I would prefer the word format. I am not from India and only vaguely familiar with how the system works.
public static string NumberToWords(int number)
{
if (number == 0) { return "zero"; }
if (number < 0) { return "minus " + NumberToWords(Math.Abs(number)); }
string words = "";
if ((number / 10000000) > 0) { words += NumberToWords(number / 10000000) + " Crore "; number %= 10000000; }
if ((number / 100000) > 0) { words += NumberToWords(number / 100000) + " Lakh "; number %= 100000; }
if ((number / 1000) > 0) { words += NumberToWords(number / 1000) + " Thousand "; number %= 1000; }
if ((number / 100) > 0) { words += NumberToWords(number / 100) + " Hundred "; number %= 100; }
if (number > 0)
{
if (words != "") { words += "and "; }
var unitsMap = new[] { "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen" };
var tensMap = new[] { "Zero", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "seventy", "Eighty", "Ninety" };
if (number < 20) { words += unitsMap[number]; }
else { words += tensMap[number / 10]; if ((number % 10) > 0) { words += "-" + unitsMap[number % 10]; } }
}
return words;
}
.. after this, Arab, Kharab, Neel etc are NOT in conventional use. Infact, even financial institutions follow this:
Once this number has been exceeded, they conveniently switch to millions/billions/trillions/quadrillions..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With