# [R] Re: Bug or new concept in formatC? -- neither

Martin Maechler maechler at stat.math.ethz.ch
Fri Oct 26 14:26:15 CEST 2001

```>>>>> "BXC" == BXC (Bendix Carstensen) <bxc at novonordisk.com> writes:

BXC> As a sequel to my previous mail on cut, formatC does not produce
BXC> what I have been taught is significant digits:

>> x <- c(1.0793,1.0796, 11.0954, 11.0736 )
>> formatC(x,digits=3,format="g")
BXC> [1] "1.08" "1.08" "11.1" "11.1"

BXC> (3,3,3,3) significant digits OK

>> formatC(x,digits=3,format="f")
BXC> [1] "1.079" "1.080" "11.095" "11.074"

BXC> (3,3,3,3) digits after point, [(4,4,5,5) significant] OK

>> formatC(x,digits=4,format="g")
BXC> [1] "1.079" " 1.08" " 11.1" "11.07"

BXC> (4,3,3,4) significant digits --- not OK (?)

BXC> Apparently formatC thinks that trailing 0s cannot count as
BXC> significant.  Is this a bug or another concept of significant
BXC> digits?

No, it's just how ISO C (ANSI C) defines the behavior of the "g" format for
(s)printf.
However, there's also a flag argument (also well defined by ISO C) which
helps here, see below.
Yes, the help(formatC) page proably could explain all this, since nowadays
not everyone has a version of Kernighan&Ritchie close at hand (I still do).
Patches for  src/library/base/man/formatc.Rd are very welcome...

What you want, Bendix, is   flag = "#" :

> formatC(c(1.0793,1.0796, 11.0954, 11.0736),digits=4,format="g")
[1] "1.079" " 1.08" " 11.1" "11.07"

> formatC(c(1.0793,1.0796, 11.0954, 11.0736),digits=4,format="g",flag="-")
[1] "1.079" "1.08 " "11.1 " "11.07"

> formatC(c(1.0793,1.0796, 11.0954, 11.0736),digits=4,format="g",flag="0")
[1] "1.079" "01.08" "011.1" "11.07"

> formatC(c(1.0793,1.0796, 11.0954, 11.0736),digits=4,format="g",flag="#")
[1] "1.079" "1.080" "11.10" "11.07"

Martin Maechler <maechler at stat.math.ethz.ch>	http://stat.ethz.ch/~maechler/
Seminar fuer Statistik, ETH-Zentrum  LEO D10	Leonhardstr. 27
ETH (Federal Inst. Technology)	8092 Zurich	SWITZERLAND
phone: x-41-1-632-3408		fax: ...-1228			<><
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help 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-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```