Skip to content

weird behavior of nsmall in format

2 messages · Adrian Dragulescu, Brian Ripley

#
[1] "0.1234567890"
[1] "0.1234568"
[1] "0.1234568"
[1] "0.12345679"

It adds zeros fine but for format(0.123456789, nsmall = 1) I want the
result to be 0.1.

I want to format numbers with a fixed number of digits.  A combination of
round and format + nsmall does the job.  sprintf will do it too, but I
wondered if the current implementation of nsmall in format is correct.

Thanks,

Adrian
#
On Mon, 13 Feb 2006, Adrian Dragulescu wrote:

            
That's not a quote from the help page.
It is, but your reading of the help page is not.  The help page 
actually says

   digits: how many significant digits are to be used for numeric and
           complex 'x'.  The default, 'NULL', uses 'getOption(digits)'.
           This is a suggestion: enough decimal places will be used so
           that the smallest (in magnitude) number has this many
           significant digits, and also to satisfy 'nsmall'. (For the
           intepretation for complex numbers see 'signif'.)

   nsmall: number of digits which will always appear to the right of the
           decimal point in formatting real/complex numbers in
           non-scientific formats.  Allowed values are '0 <= nsmall <=
           20'.

Note the `always'.