13

As in the title, does anyone know if there's a place in .NET, or a 3rd party library, where integers can be converted to their "ordering" counterparts.

1 - first
2 - second
3 - third
etc...

I could of course just write one myself, but I'd rather reuse something already existing if possible.

Thanks.

MgSam
  • 12,139
  • 19
  • 64
  • 95
  • This is very closely related to http://stackoverflow.com/questions/20156/is-there-an-easy-way-to-create-ordinals-in-c, and the answers there seem to indicate there is no easy built in way. – Steve Sep 18 '12 at 14:45
  • possible duplicate of [converting numbers in to words C#](http://stackoverflow.com/questions/2729752/converting-numbers-in-to-words-c-sharp) – Alexei Levenkov Sep 18 '12 at 16:42
  • possible duplicate of [How can I convert an integer into its verbal representation?](http://stackoverflow.com/questions/554314/how-can-i-convert-an-integer-into-its-verbal-representation) – Adi Lester Nov 03 '12 at 12:50
  • Do you have a maximum number? – The_Black_Smurf Jul 07 '14 at 17:47

3 Answers3

15

Humanizer is a great library for this: Humanizr

Install-Package Humanizer

With Humanizer, you'd do:

int number = 5;
string ordinal = number.ToOrdinalWords();
Chris Hynes
  • 9,999
  • 2
  • 44
  • 54
  • 1
    This. I rolled my own library for this, but you will run into language problems pretty fast - the logic how a number is converted to a string differs between languages – Christian Sauer Jul 07 '14 at 17:33
  • Does this operate in the opposite direction though? -- words -> numbers – ryanwebjackson Oct 21 '19 at 17:47
  • Nope, looks like it doesn't right now. It can convert other humanized things like enums but not number ordinals. Submit a PR ;-P – Chris Hynes Dec 13 '19 at 15:46
1

converting numbers in to words C#

Similiar to this question. You can get what you need from LukeH's answer:

public static string NumberToWords(int number)
{
if (number == 0)
    return "zero";

if (number < 0)
    return "minus " + NumberToWords(Math.Abs(number));

string words = "";

if ((number / 1000000) > 0)
{
    words += NumberToWords(number / 1000000) + " million ";
    number %= 1000000;
}

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;
}
Community
  • 1
  • 1
Darren
  • 68,902
  • 24
  • 138
  • 144
  • I think you should simply dup it against that question instead of LukeH's answer. Also as noted there it is pretty much en-US only... Other languages/countries would add more fun/complexity. – Alexei Levenkov Sep 18 '12 at 16:44
0

I'm unaware of a pre-existing library to handle this, but there a great simple method here: .NET convert number to string representation (1 to one, 2 to two, etc…)

Community
  • 1
  • 1
user902553
  • 38
  • 4