Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R shows too few digits

Tags:

rounding

r

digits

I wanted to calculate that term:

(21000*(1.022^7-1))/(1.022-1)

R tells me, it's 157065.7. This was my answer to a task that I had to solve. It was stated that this answer is not correct, the correct answer should be 157065.67. Then I calculated this with my calculator on my smartphone and indeed, the solution was 157065.67034. I am really wondering what is wrong with R and whether I can change some options. I've tried options()$digits but that's 7, this can't be the problem...

like image 271
TobiSonne Avatar asked Nov 26 '25 12:11

TobiSonne


2 Answers

One option:

formatC((21000*(1.022^7-1))/(1.022-1), digits = 11)
#> [1] "157065.67035"

Created on 2021-06-24 by the reprex package (v2.0.0)

like image 129
Ian Gow Avatar answered Nov 30 '25 19:11

Ian Gow


Use options(digits = 15). Note that this changes globally the format. Also, the number of digits indicates all the digits (before and after the decimal separator).

like image 22
Pedro Alencar Avatar answered Nov 30 '25 20:11

Pedro Alencar