Skip to content
Prev 181014 / 398502 Next

round function seems to produce maximum 2 decimals

On 20-May-09 20:10:15, Glenn E Stauffer wrote:
What you're missing is that when you do (e.g.)

  h <- 12345.16711
  round(h,digits=4)
# [1] 12345.17

what is displayed ("[1] 12345.17") is not the result of round(),
but what the result of round() is to be displayed as given the
options digits=7 (default) for the number of *significant figures*
in the display of stored values.

To see the result as it is stored, you should use print() with
the appropriate number of disgits specified:

  print( round(h,digits=5),10)
# [1] 12345.16711
  print( round(h,digits=4),10)
# [1] 12345.1671
  print( round(h,digits=3),10)
# [1] 12345.167
  print( round(h,digits=2),10)
# [1] 12345.17

Internally, round(h) is correctly stored:

  h4 <- round(h,4)
  h - h4
# [1] 1e-05
  h3 <- round(h,3)
  h - h3
# [1] 0.00011
  h2 <- round(h,2)
  h - h2
# [1] -0.00289

To illustrate the influence of the display option digits=7:

  h<-45.16711
  h
# [1] 45.16711
  round(h,digits=4)
# [1] 45.1671
   round(h,digits=3)
# [1] 45.167
  round(h,digits=2)
# [1] 45.17

  h<-345.16711
  h
# [1] 345.1671
   round(h,digits=4)
# [1] 345.1671
   round(h,digits=3)
# [1] 345.167
   round(h,digits=2)
# [1] 345.17

  h<-2345.16711
  h
# [1] 2345.167
  round(h,digits=4)
# [1] 2345.167
   round(h,digits=3)
# [1] 2345.167
  round(h,digits=2)
# [1] 2345.17

Hoping this helps,
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 20-May-09                                       Time: 22:54:41
------------------------------ XFMail ------------------------------