# [R-SIG-Finance] Ques regarding price conversion to 32nds

Pierre Lapointe pierrelap at gmail.com
Thu Sep 2 00:45:51 CEST 2010

```Thanks,

So here's the full function to convert fractions to decimals

#Fractions to decimals
x <- c("2 1/32","7 7/32","4 27/32")

dec.fun <-function(x){
my.split<-strsplit(x," ")
my.mat <-matrix(unlist(my.split),,2,byrow=TRUE)
my.eval <-function(y){
eval(parse(text=paste(y,collapse="+")))
}
apply(my.mat,1,my.eval)
}

dec.fun(x)

This will need work if you feed it data.frames or matrices. And it
does not work if there is no integer, only a fraction. But you get the
idea...

HTH

Pierre

On Wed, Sep 1, 2010 at 6:15 PM, Joshua Ulrich <josh.m.ulrich at gmail.com> wrote:
>
> Here's the answer to the second part, using the results from Pierre's email:
>
> > eval(parse(text=paste(my.split[[1]],collapse="+")))
> [1] 2.03125
>
> Best,
> --
> Joshua Ulrich
>
>
> On Wed, Sep 1, 2010 at 5:09 PM, Pierre Lapointe <pierrelap at gmail.com> wrote:
> > Here's the answer to the first part of your question:
> >
> > #Decimals to fractions
> > frac.fun <- function(x, den){
> > dec <- seq(0, den) / den
> > nams <- paste(seq(0, den), den, sep = "/")
> > sapply(x, function(y) nams[which.min(abs(y - dec))])
> > }
> >
> > frac.fun(c(0, 1, 0.8266667, .066666, 0.2666666,0.03125), 32)
> >
> > To convert from fraction to decimal, I would strsplit numbers in two parts,
> > one integer and one fraction.
> >
> > #Fractions to decimals
> > x <- c("2 1/32","3 7/32","4 27/32")
> > my.split<-strsplit(x," ")
> >
> > However, I do not know how to convert a "1/32" caracter string to numeric.
> >
> > Pierre
> >
> >
> > On Wed, Sep 1, 2010 at 5:22 PM, Gandhi, Puneet - RSCH AMRS <
> > p.gandhi at baml.com> wrote:
> >
> >>
> >>
> >>
> >> Hi,
> >>
> >> Can any one point me to a function which converts prices in decimals to
> >> 32nds and vice versa.
> >>
> >> Thanks
> >> Puneet
> >>
> >> ----------------------------------------------------------------------
> >> This message w/attachments (message) is intended solely ...{{dropped:7}}
> >>
> >> _______________________________________________
> >> R-SIG-Finance at stat.math.ethz.ch mailing list
> >> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> >> -- Subscriber-posting only. If you want to post, subscribe first.
> >> -- Also note that this is not the r-help list where general R questions
> >> should go.
> >>
> >
> >        [[alternative HTML version deleted]]
> >
> > _______________________________________________
> > R-SIG-Finance at stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> > -- Subscriber-posting only. If you want to post, subscribe first.
> > -- Also note that this is not the r-help list where general R questions should go.
> >

```