45

This is a wild one: can anyone write a code that converts a number into the corresponding word? e.g.:

4 -> 'four'
31 -> 'thirty-one'

and so on. Obviously, it is not enough to build an array in which you can store the numbers and the word associated with it and then just print instead of the number the stored word. What complicates matters more, are numbers like 11 ('eleven'). I intend to use it for numbering equations. So, anybody up to it?

3 Answers 3

38

You can use the fmtcount package to achieve that:

\documentclass{minimal}
\usepackage{fmtcount}

\begin{document}

4: \numberstringnum{4}

31: \numberstringnum{31}

\end{document}

Use \Numberstringnum and \NUMBERstringnum respectively for capitalized and full-caps versions.

2
  • 2
    Concerning I18N: If \usepackage[ngerman]{babel} is in the document header, then the numbers are output in German.
    – feklee
    Oct 17, 2013 at 14:12
  • 1
    @feklee and what should I do if language which I need in babel is not supported by this package? Package fmtcount Warning: No support for \numberstring for language 'lithuanian ' on input line 176.
    – holms
    Feb 29, 2016 at 4:24
18

The moreenum package already does this as well.

\documentclass{article}
\usepackage{moreenum}
\newcounter{mycounter}
\renewcommand{\themycounter}{\Nthwords{mycounter}}
\begin{document}

\setcounter{mycounter}{11}
\themycounter

\setcounter{mycounter}{470}
\themycounter

\end{document}

Result:

enter image description here

Use \nthwords for lowercase. I think in the next version of the package, these will change to \nwords and \Nwords.

6
  • 3
    Hmmm... CTAN has a full documentation for fmtcount. The same is not true for moreenum. Plus fmtcount has more macros. Thanks anyway for your suggestion, I really appreciate you took your time to look into it!
    – Count Zero
    Sep 13, 2011 at 15:21
  • texdoc moreenum gives me documentation; not sure why it's not showing up on CTAN.
    – frabjous
    Sep 13, 2011 at 15:56
  • 1
    moreenum actually uses fmtcount internally. The documentation doesn't show up on that CTAN page, but if you click the CTAN directory link, you get moreenum-testcase.pdf which also serves as documentation. In fact, the package is simple enough that basically everything you need to know is in the readme anyway...
    – Seamus
    Nov 3, 2011 at 8:28
  • Oh yeah. The version of moreenum on CTAN still has \nwords and \nthwords the wrong way round! The version on github has this fixed. I will upload a new version to CTAN today. (which will also have the documentation renamed so that CTAN finds it.)
    – Seamus
    Nov 3, 2011 at 8:45
  • @Seamus The description for moreenum on CTAN still says it depends on nth and not fmtcount.
    – bb010g
    Jan 1 at 16:55
18

fmcount only works with numbers up to 100,000. For larger numbers, you will need to use numname which borrows the code from the memoir class. From what I recall, this can deal with bigger numbers. Neither the readme nor the package documentation actually tells you what the commands to use are, so here they are:

  • \numtoname turns a number into lowercase words (e.g. one)
  • \numtoName as above, but the first letter is capitalised
  • \NumToName as above but all words are capitalised
  • \ordinaltoname turns a number into lc ordinal (e.g. first)
  • \ordinaltoName as above but first letter capitalised
  • \OrdinalToName as above but all words capitalised

This package seems to manage all numbers up to TeX's limit of 2147483647. (numname helfully informs me that this is: two billion, one hundred and forty-seven million, four hundred and eighty-three thousand, six hundred and forty-seven)

4
  • How to remove the hyphen in thirty-one?
    – David
    Aug 26, 2020 at 6:00
  • I haven't tested this, but from looking at numname.sty you could try adding \renewcommand*\tensunitsep{ } to your preamble (below having called numname
    – Seamus
    Aug 26, 2020 at 9:02
  • How do I convert a variable's value to a word, e.g., \numtoname{\someVar}? Apr 2, 2021 at 21:02
  • That depends how \someVar is defined and might involve some expansion shenanigans. In any case, I think that's a different question, perhaps you should ask it.
    – Seamus
    Apr 3, 2021 at 6:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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