Rounding error R - r

When I run this calculation: 2.36*7299.83 in R is returning 17227.599 but the correct answer should be 17227.5988. Even when I run options(digits=8) before or round(2.36*7299.83, 4) it still gets the wrong answer.
It does not look like floating point error, because it is in the fourth decimal place.
Any help?
Thank you.

I think you have to specify the format:
formatC(2.36*7299.83, digits=4, format = "f")
"17227.5988"
formatC(2.36*7299.83, digits=8, format = "f")
"17227.59880000"

the default digits is 7.
getOption("digits")
# [1] 7
print(round(2.36*7299.83, 4), digits=9)
# [1] 17227.5988

Related

R print a number with decimal places and the number must NOT be in string format

I'm working on a HackerRank samples. For some reasons, the output needs to have 1 decimal place even if the actual number is a round number.
For example, even though my answer is theoretically correct, which is 32, HackerRank considers it as false because 32 is not 32.0
I've also tried
format(32, nsmall = 1)
But this is still an error because the output comes with " ".
I've looked into Formatting Decimal places in R, but this doesn't answer my question.
The output must not be "32.0", it must be 32.0
Thanks #Roland for the tips. The below is accepted.
cat(format(32, nsmall = 1))
I Think below code should work:
# devtools::install_github("renkun-ken/formattable")
library(formattable)
p <- formattable(as.numeric("32"), digits = 1, format = "f")
p

Why does R behave like this?

I use the following multiplication in R (v. R-3.6.1): 115*1.044. I get 120.1. In Excel I get 120.06. By hand I get 120.062.
I select use options(digits=4) in R, but I still get the same result: 120.1.
Why does R behave like this? I use to trust it more than Excel, but it seems that here Excel is more accurate in what it returns. Is there a way to force R to return the accurate digits I would get if multiplying by hand?
The function format has the digits option referred to the total digits of the number considered as a whole (integer and decimal part):
> format(115*1.044, digits = 5)
[1] "120.06"
> format(115*1.044, digits = 4)
[1] "120.1"

R read excel file numeric precision problem

I have a number in an excel file that is equal to -29998,1500000003
When I try to open it in R I get
> library(openxlsx)
> posotest <- as.character(read.xlsx("sofile.xlsx"))
> posotest
[1] "-29998.1500000004"
Any help? Desired result: -29998,1500000003
EDIT: with options(digits=13) I get -29998.150000000373 which could explain why the rounding is done, however even with options(digits=13) I get
> as.character(posotest)
[1] "-29998.1500000004"
Do you have any function that would allow me to get the full number in characters?
EDIT2 format does this but it adds artificial noise at the end.
x <- -29998.150000000373
format(x,digits=22)
[1] "-29998.15000000037252903"
How can I know how many digits to use in format since nchar will give me a wrong value?
The file is here
You can get a string with up to 22 digits of precision via format():
x <- -29998.150000000373
format(x,digits=22)
[1] "-29998.15000000037252903"
Of course, this will show you all sorts of ugliness related to trying to represent a decimal number in a binary representation with finite precision ...

convert number from scientific notation in R

I have a numeric variable imported from Oracle with 17 numbers, for example: 20172334534654667.
Now I imported it from Oracle using dbGetQuery() in R, but R use scientific notation: 2.01723e+16
If I try to convert the number using:
mydata$var <- format(mydata$a, scientific=FALSE)
I obtain 20172334534654600 instead of 20172334534654667
So, the last two numbers are always substituted with 00.
How can I solve it, possibly without using additional packages?
I was unable to replicate your issue, but I think it would probably be best to use formatC rather than format.
For your case, it could be:
numb <- 20172334534654667
numb
formatC(numb, format = "f", digits = 0)
Which gives:
[1] "20172334534654668"
Hopefully that works for you!

knitr - strange behaviour for digits

I ran into some trouble concerning the number of digits printed in knitr.
The number does not correspond to the settings [options('digits')].
I know that it was an issue with that about a year ago but has been resolved (https://github.com/yihui/knitr/issues/120).
```{r}
packageVersion("knitr")
options("digits")
a <- 100.101
a
as.character(a)
options(digits=4)
a
options(digits=10)
a
```
This is what I get (the same on two different machines): http://rpubs.com/markheckmann/6715 .
Something is going wrong here and I do not have a clue. Any ideas?
I don't think options(digits=10) is doing what you exepct. Perhaps you meant
sprintf( "%.10f",101.101)
# [1] "101.1010000000"
This isn't a knitr issue; it's just how R displays digits. Try your code on its own, without knitting.
a <- 100.101
a
#[1] 100.101
as.character(a)
#[1] "100.101"
options(digits=4)
a
#[1] 100.1
options(digits=10)
a
[1] 100.101
print doesn't pad numbers with zeroes to make up the width; for that you need format.
format(a, nsmall = 10)
#[1] "100.1010000000"

Resources