Re: [R] R function to convert a number to text

From: Jim Lemon <>
Date: Mon, 03 Mar 2008 21:38:12 +1100

lin tang wrote:
> hi, Dear R users -
> I wonder is there a written R function to convert a number to a text, say convert 1 to "one" , 100 to "one hundred". I know in xls. has such a function BAHTTEXT, does anybody know is there a similar function in R ? Thanks.
Hi Lin,
I got bored after the millions, but this should handle the smaller numbers, and you can always extend it.

digits2text<-function(x,mult="") {

   "fifteen","sixteen","seventeen","eighteen","nineteen")   tens<-c("ten","twenty","thirty","forty","fifty",    "sixty","seventy","eighty","ninety")
  digits<-rev(as.numeric(strsplit(as.character(x),"")[[1]]))   digilen<-length(digits)
  if(digilen == 2 && digits[2] == 1) return(teens[digits[1]+1])   digitext<-units[digits[1]]
  if(digilen > 1) digitext<-c(digitext,tens[digits[2]])
  if(digilen > 2) digitext<-c(digitext,"hundred",units[digits[3]])
  if(digilen > 3) digitext<-
  if(digilen > 6) digitext<-
  return(paste(c(rev(digitext),mult),sep="",collapse=" ")) }

Jim mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Mon 03 Mar 2008 - 10:35:09 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 03 Mar 2008 - 12:30:19 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive