From brahm@alum.mit.edu  Wed Mar 13 18:05:14 2002
From: brahm@alum.mit.edu (David Brahm)
Date: Wed, 13 Mar 2002 13:05:14 -0500
Subject: [Rd] Commas in formatC
Message-ID: <15503.38106.38626.976198@gargle.gargle.HOWL>

formatC() is great for formatting numbers!  But it would be even better if it
could optionally insert commas (or semicolons), e.g.
 R> formatC(1234567.89, digits=2, format="f", commas=T)
    [1] "1,234,567.89"

Here's a snippet of code that does that, which could more or less just be
inserted into at the end of formatC if any R-core guru were so inclined.  "r"
is the result so far, and "commas" is a logical flag:

  stp <- regexpr("[0-9]+", r);  len <- attr(stp, "match.length")
  if (commas && (maxl <- max(len)) > 3) for (i in seq(3, maxl-1, 3))
    r[len>i] <- paste(substring(r[len>i], 1, (stp+len-1-i)[len>i]),
                      substring(r[len>i], (stp+len-i)[len>i]), sep=",")
-- 
                              -- David Brahm (brahm@alum.mit.edu)
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._


[Rd] Commas in formatC
David Brahm