Convert Numbers to Words
num2words numvar [if exp] [in range] [ , generate(newvar) round ordinal ]
Description
num2words converts numbers (including real and ordinal numbers) in a single numvar to text. It is useful for converting numbers to text for insertion in graph or table titles, captions, or labels; value or variable labels; charactersics or notes; or, this utility can convert numbers to words for inclusion in the text of a document created from Stata via commands like file write, texdoc(if installed), or rtfutil(if installed).
num2words converts:
1. positive or negative numbers, including fractional numbers;
2. numbers up to 999 trillion (15 places to the left of the decimal);
3. fractional numbers up to the millionths place (6 places to the right of the decimal);
Converted text will be automatically truncated if the conversion is longer than Stata's character limit for string variables (See help limits and help data_types for more).
Options
generate() specifies the new string variable containing the converted text for numvar. If no variable name is specified the default is numvar2.
round tells num2words to ignore fractional part during conversion to text.
ordinal adds the appropriate suffix to the cardinal number (fractional part ignored) to form ordinal number (421 converted to 421st or 2 converted to 2nd).
if and in are allowed.
Examples
//Create some data// . clear . set obs 10 . g x = round(runiform()*100, .05) . g x2 = int(runiform()*100) . replace x = -2.5 in 1
//Convert Numbers to Text// . num2words x, g(x_converted) . num2words x, g(x_rounded) round . replace x_converted = proper(x_rounded) . num2words x, g(x2_ordinal) ordinal
//Use Converted Text in Graph// . egen mx = mean(x) . num2words mx, round . gr bar x , over(x2_ordinal, sort(1)) /// note( X for Obs 2 is `=x_rounded[2]') /// text(60 20 `"Mean is `=mx2'"', box )
Author
Eric A. Booth, Texas A&M University ebooth@ppri.tamu.edu http://www.eric-a-booth.com
Also see
On-line: help for string_functions; toroman (if installed); fromroman (if installed); tostring