ALB
ALB

Reputation: 41

Converting Decimal to different bases

I'm looking for a way to convert decimals to other bases in R. The function dec2base from the oro.dicom package only works for integers (as stated in its help file):

> dec2base(1.559,9)
[1] "1"

Whereas 1.559 in base 9 should be 1.5024, and I was wondering if there is another method.

Upvotes: 4

Views: 1427

Answers (2)

Ben Bolker
Ben Bolker

Reputation: 226881

The Rmpfr package can do arbitrary-precision arithmetic and base conversion:

library(Rmpfr)
x <- mpfr(1.559, precBits = 64, base = 10)  ## base = 10 is default
b9 <- as.numeric(format(x, base = 9))
print(b9)
## 1.502453

Upvotes: 1

ojblass
ojblass

Reputation: 21630

I think that you would be able to multiply the number by the number of non zero decimal places and undo the operation on the backend of the dec2base command.

Upvotes: 0

Related Questions