Right now I’m building a small app that imports data from a spreadsheet and due to the nature of the original entry, there is a string being read in that has values such as 8½, 2½, etc.
My goal with a simple function is to convert 2½ into float 2.5, for example.
I’ve tried the .to_f method but that has left me with a weird value of 2.02½.
Any insight or suggestions here would be very much appreciated!
Unicode only supports a small number of vulgar fractions so a simple lookup table will do the trick:
Then, a little bit of regex wrangling to pull your “number” apart:
And finally, put them together taking care to properly deal with possible
nils from the regex:Remember that
nil.to_iis0andnil.to_fis0.0.