Skip to content
Prev 7406 / 63421 Next

prettyNum inserts leading commas (PR#1548)

Under R-1.5.0 on Solaris 2.6:

R> prettyNum(123456789, big.mark=",")
   [1] ",123,456,789"

and that bad behavior (leading comma) spills into formatC as well:

R> formatC(123456789, digits=0, format="f", big.mark=",")
   [1] ",123,456,789"

Looks to me like a bug in src/library/base/R/format.R, in function prettyNum:

  B.[i.big] <- revStr(gsub(P0("([0-9]{",big.interval,"})"),          # Original
                           P0("\\1",big.mark), revStr(B.[i.big])))

where we reverse the "before-the-decimal" string, then put a comma after each
set of 3 digits.  One solution is to make the first regexp check that it is not
followed by a word boundary, i.e. add "\\B" to the end of it:

  B.[i.big] <- revStr(gsub(P0("([0-9]{",big.interval,"})\\B"),       # DB's fix
                           P0("\\1",big.mark), revStr(B.[i.big])))

which seems to correct the problem:

R-with-fix> prettyNum(123456789, big.mark=",")
            [1] "123,456,789"