1

Does anyone knows how to convert an integer to a long ordinal word in C# ?

Is easy to convert 1 to 1st, 2 to 2nd using this algorithm described here: Is there an easy way to create ordinals in C#?. But i need a way to convert 1 to the long ordinal word version. For example:

1 => First

2 => Second

3 => Third ...

9 => Ninth and so on for any number.

The solution can not either create an infinite dictionary list with key par values of (1, "first", 2, "second", etc, etc)

love2code
  • 441
  • 6
  • 9
  • 4
    Check out this -> https://github.com/Humanizr/Humanizer#number-to-ordinal-words – Ofir Winegarten Feb 24 '18 at 19:08
  • Thanks humanizr works, This class https://github.com/Humanizr/Humanizer/blob/dev/src/Humanizer/Localisation/NumberToWords/EnglishNumberToWordsConverter.cs will contain the desired result – love2code Feb 24 '18 at 19:31
  • By the way this was not a homework, This was actually requested by a product analyst from the company that a work for. – love2code Feb 24 '18 at 19:37

1 Answers1

1

I too cant find the exact solution for that.But I managed to get the answer.Try this,It will be useful.

public string ConvertNumbertoWordsFirst(long number)
    {
        string words = "";
        words = ConvertNumbertoWords(number);
        words = words.TrimEnd('\\');
        if (words.EndsWith("One"))
        {
            words = words.Remove(words.LastIndexOf("One") + 0).Trim();
            words = words + "First";
        }
        else if (words.EndsWith("Two"))
        {
            words = words.Remove(words.LastIndexOf("Two") + 0).Trim();
            words = words + "Second";
        }
        else if (words.EndsWith("Three"))
        {
            words = words.Remove(words.LastIndexOf("Three") + 0).Trim();
            words = words + "Third";
        }
        else if (words.EndsWith("Five"))
        {
            words = words.Remove(words.LastIndexOf("Five") + 0).Trim();
            words = words + "Fifth";
        }
        else if (words.EndsWith("Eight"))
        {
            words = words.Remove(words.LastIndexOf("Eight") + 0).Trim();
            words = words + "Eighth";
        }
        else if (words.EndsWith("Nine"))
        {
            words = words.Remove(words.LastIndexOf("Nine") + 0).Trim();
            words = words + "Ninth";
        }
        else
        {
            words = words + "th";
        }
        return words;
    }

ConvertNumbertoWords() is the function i got it from the below link. Link : converting numbers in to words C#

public string ConvertNumbertoWords(long number)
    {
        if (number == 0) return "Zero";
        if (number < 0) return "minus " + ConvertNumbertoWords(Math.Abs(number));
        string words = "";
        if ((number / 100000) > 0)
        {
            words += ConvertNumbertoWords(number / 100000) + " Lakhs ";
            number %= 100000;
        }
        if ((number / 1000) > 0)
        {
            words += ConvertNumbertoWords(number / 1000) + " Thousand ";
            number %= 1000;
        }
        if ((number / 100) > 0)
        {
            words += ConvertNumbertoWords(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;
    }
santhosh
  • 11
  • 1