5
$\begingroup$

I am trying to create a function that does:

f[1]
(* "first" *)

f[27]
(* "twentyseventh" *)

f[50]
(* "fiftieth *)

There need not be a hyphen or space in "twentyseventh".

I searched "OrdinalNumber" (Interpreter type), IntegerName, and SpokenString, but none of these seem to generate actual ordinal numbers composed of words only.

$\endgroup$
1
  • $\begingroup$ I wrote a python script to do this. Right now it converts pretty large numbers but I want it to theoretically go up to infinity. Here's a link to the script on github: github.com/stephen-wolfe/python-scripts/blob/master/… $\endgroup$ – user62600 Jan 26 '19 at 17:27
5
$\begingroup$

IntegerName as of Version 10 should fit the bill:

IntegerName[ 1, "Ordinal" ]

first

IntegerName[#, "Ordinal" ]& /@ {27, 50} 

{"twenty-seventh", "fiftieth"}

It even produces word-only answers for longer ordinal numbers:

IntegerName[ 10000027, "Ordinal" ]

"ten million twenty-seventh"

Building a custom converter function (word-only) to OP's demands

The OP wants to remove Whitespace and - from the ordinal string which can be done using StringReplace within a custom function:

f[ i_Integer ] := With[
    {
        ordinalStringRules = StringReplace[ Whitespace | "-" -> "" ]
    }
    ,
    IntegerName[ i, "Ordinal" ] // ordinalStringRules
]

f[ 10000027 ]

"tenmilliontwentyseventh"

$\endgroup$
2
  • $\begingroup$ @JungHwan Does this answer your question? $\endgroup$ – gwr Jan 30 '19 at 13:02
  • $\begingroup$ For a different, maybe more general, take on this problem see my answer here. $\endgroup$ – gwr Jan 31 '19 at 11:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.