Skip to content Skip to sidebar Skip to footer

Is There A Library To Convert Integer To First/second/third

I have a list of integers - is there a library to convert them into plain text ranking? IE: 1,2,3 -> 'first, second, third' ?

Solution 1:

The python inflect package has a method for converting numerals into ordinals:

import inflect
p = inflect.engine()

for i in range(1,25,5):
    print(p.ordinal(i))

displays:

1st
6th
11th
16th
21st

Solution 2:

How high are you planning on going? (Do you ever expect higher than, say, "twentieth"?)

Maybe you just need a dict,

nth = {
    1: "first",
    2: "second",
    3: "third",
    4: "fourth"# etc
}

Solution 3:

can't comment on ryvantage's post because of points but I wrote the same code for python:

def appendInt(num):
    if num > 9:
        secondToLastDigit = str(num)[-2]
        if secondToLastDigit == '1':
            return'th'
    lastDigit = num % 10if (lastDigit == 1):
        return'st'
    elif (lastDigit == 2):
        return'nd'
    elif (lastDigit == 3):
        return'rd'else:
        return'th'



def appendInt2(num):
    value = str(num)
    if len(value) > 1:
        secondToLastDigit = value[-2]
        if secondToLastDigit == '1':
            return'th'
    lastDigit = value[-1]
    if (lastDigit == '1'):
        return'st'
    elif (lastDigit == '2'):
        return'nd'
    elif (lastDigit == '3'):
        return'rd'else:
        return'th'

The second is more of a direct translation, but I found that the first variation is considerably faster:

Fri Aug 28 11:48:13 2015 results

300000005functioncallsin 151.561 secondsOrderedby: callcount, name/file/linencallstottimepercallcumtimepercallfilename:lineno(function)1000000006.6740.0006.6740.000 {len}
10000000043.0640.00043.0640.000 test.py:7(appendInt)
10000000066.6640.00073.3390.000 test.py:22(appendInt2)
        10.0000.0000.0000.000 {method 'disable' of '_lsprof.Profiler' objects}
        10.0000.000151.561151.561 <string>:1(<module>)
        10.0000.000151.561151.561 test.py:51(main)
        116.73716.73759.80159.801 test.py:39(test1)
        118.42118.42191.75991.759 test.py:45(test2) 

Solution 4:

Similarly to the vein @Hugh Bothwellwas following, only not using a dict (since your keys are already nice and numerical), I've put together the following "one liner":

ordinal=lambda x:["first","second","third","fourth","fifth","sixth","seventh","eighth","ninth","tenth","eleventh","twelfth"][x-1]

Which covers all cases up to 12. If you need higher (into the hundreds, etc), then you'll probably need something more robust (with recursion I'd imagine, etc).

Solution 5:

Depending on your situation, you might find it useful to retain the integer and append "st", "nd", "rd", and "th". If so, here is a simple algorithm:

NOTE: This algorithm is written in Java. I don't know Python. Anyone who wants to re-write it in Python, be my guest.

publicstaticStringappendInt(int number) {
    String value = String.valueOf(number);
    if(value.length() > 1) {
        // Check for special case: 11 - 13 are all "th".// So if the second to last digit is 1, it is "th".
        char secondToLastDigit = value.charAt(value.length()-2);
        if(secondToLastDigit == '1')
            return"th";
    }
    char lastDigit = value.charAt(value.length()-1);
    switch(lastDigit) {
        case'1':
            return"st";
        case'2':
            return"nd";
        case'3':
            return"rd";
        default:
            return"th";
    }
}

So

System.out.println(1 + appendInt(1));
System.out.println(2 + appendInt(2));
System.out.println(3 + appendInt(3));
System.out.println(4 + appendInt(4));
System.out.println(5 + appendInt(5));
System.out.println(11 + appendInt(11));
System.out.println(21 + appendInt(21));

displays

1st
2nd
3rd
4th
5th
11th
21st

Post a Comment for "Is There A Library To Convert Integer To First/second/third"