I have an Excel column containing part numbers. Here is a sample
As you can see, it can be many different datatypes: Float
, Int
, and String
. I am using roo
gem to read the file. The problem is that roo
interprets integer cells as Float
, adding a trailing zero to them (16431 => 16431.0). I want to trim this trailing zero. I cannot use to_i
because it will trim all the trailing numbers of the cells that require a decimal in them (the first row in the above example) and will cut everything after a string char in the String
rows (the last row in the above example).
Currently, I have a a method that checks the last two characters of the cell and trims them if they are ".0"
def trim(row)
if row[0].to_s[-2..-1] == ".0"
row[0] = row[0].to_s[0..-3]
end
end
This works, but it feels terrible and hacky. What is the proper way of getting my Excel file contents into a Ruby data structure?
or, from string: