Friends I am trying to format a number to a string so 2189.745 goes to "2,189.35" and 309283.929 goes to "309,283.93" I have tried to use formatC(X, big.mark=",",drop0trailing=FALSE, format="f") but it does not get the number of decimals correct. Specifying digits does not work as that is significant digits. I could use a switch statement switching on the size of the number to be formated to set the digits parameter but that is complex and leaves me insecure that thre may be other problems I have not uncovered. Is there a simple way of using library functions to insert big.marks and get the number of trailing digits correct? Perhaps some way of using sprintf? cheers Worik [[alternative HTML version deleted]]
On Sun, May 8, 2011 at 4:41 PM, Worik R <worikr at gmail.com> wrote:> Friends > > I am trying to format a number to a string so 2189.745 goes to "2,189.35" > and 309283.929 goes to "309,283.93" > > I have tried to use formatC(X, big.mark=",",drop0trailing=FALSE, format="f") > but it does not get the number of decimals correct. ?Specifying digits does > not work as that is significant digits. ?I could use a switch statement > switching on the size of the number to be formated to set the digits > parameter but that is complex and leaves me insecure that thre may be other > problems I have not uncovered. > > Is there a simple way of using library functions to insert big.marks and get > the number of trailing digits correct? ?Perhaps some way of using sprintf? >Try this inserting a round() step before the final printing: x = 309283.929 formatC(round(x, 2), big.mark=",",drop0trailing=TRUE, format="f") [1] "309,283.93" HTH, Peter> cheers > Worik > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Sent from my Linux computer. Way better than iPad :)
On 2011-05-08 16:41, Worik R wrote:> Friends > > I am trying to format a number to a string so 2189.745 goes to "2,189.35" > and 309283.929 goes to "309,283.93" > > I have tried to use formatC(X, big.mark=",",drop0trailing=FALSE, format="f") > but it does not get the number of decimals correct. Specifying digits does > not work as that is significant digits. I could use a switch statement > switching on the size of the number to be formated to set the digits > parameter but that is complex and leaves me insecure that thre may be other > problems I have not uncovered.I see that you've had the solution you seek in terms of round(), but what exactly is wrong with the 'digits' argument? It works for me in the cases you cite. (Note that format = "f" is handled differently from other formats, according to ?formatC.) Peter Ehlers> > Is there a simple way of using library functions to insert big.marks and get > the number of trailing digits correct? Perhaps some way of using sprintf? > > cheers > Worik > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.