gaut
gaut

Reputation: 5958

R read excel file numeric precision problem

I have a number in an excel file that is equal to -29998,1500000003

excelfile

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

Upvotes: 2

Views: 1991

Answers (1)

Ben Bolker
Ben Bolker

Reputation: 226951

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 ...

Upvotes: 3

Related Questions