I'm nubbie in rails and ruby In my app i get data from csv and in some fields of quantities value is 100> and etc. But how to check that? and select only integer(float) part? But note! it can be <20, >30, 30< and so over.
2 Answers
If you have floating point numbers (not just the integer part), you can use a small variant of Justin Ko's solution:
values = ["10.1>", "<20.3", ">30.4", "30.6<"]
values.each do |val|
puts /(\d+\.\d+)/.match(val)[0].to_f
end
# => Output will be 10.1 20.3 30.4 30.6
3 Comments
Anupam
@pavelby, you can then use
puts /(\d+\.?\d*)/.match(val)[0].to_f as the regexp. The numbers will be returned as floats, but this pattern will catch the integers and the floating numbers as well.byCoder
but also how to catch such numbers? so if 10 didn't have ><= we go more, but if has go to you solution?
Anupam
@PavelBY, I am not completely sure I understood your question: however, the regexp in my comment above with catch all the numbers (integers as well as the floating point numbers.)