specify the number of decimal numbers
On 14-May-09 15:15:16, James W. MacDonald wrote:
Wacek Kusnierczyk wrote:
(Ted Harding) wrote:
On 14-May-09 12:27:40, Wacek Kusnierczyk wrote:
... but remember that sprintf introduces excel bugs into r (i.e.,
rounding is not done according to the IEC 60559 standard, see
?round):
ns = c(0.05, 0.15)
round(ns, 1)
# 0.0 0.2
as.numeric(sprintf('%.1f', ns))
# 0.1 0.1
vQ
True! And thanks for importing that point into the discussion.
said but true, true but sad. i have already raised the issue on this list earlier, but to no response. apparently, this sort of excel bug in r is an intentional feature, so you may not get it improved anytime soon. unless you submit a patch and get it accepted, that is.
Have you brought this 'issue' up with the Perl people as well?
perl -e 'print sprintf("%.1f", 0.05) . "\n";'
0.1
perl -e 'print sprintf("%.1f", 0.15) . "\n";'
0.1
This happens also when you use C's fprintf and sprintf (at any rate
in my gcc):
#include <stdio.h>
#include <math.h>
main(argc,argv) int argc; char **argv;
{
fprintf(stdout, "%.1f\n", 0.15);
fprintf(stdout, "%.1f\n", 0.05);
fprintf(stdout, "%.2f\n", 0.15);
fprintf(stdout, "%.2f\n", 0.05);
}
cc -o testprint3 testprint3.c
./testprint
0.1
0.1
0.15
0.05
(with similar output when printing a string formatted by sprintf).
So, in so far a R relies on the compiler's implementation of the
*printf functions, this can hardly be put right withbout re-writing
[g]cc!
Ted.
--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 14-May-09 Time: 17:16:40
------------------------------ XFMail ------------------------------