Trim a trailing .0

前端 未结 4 1079
我在风中等你
我在风中等你 2021-02-19 01:02

I have an Excel column containing part numbers. Here is a sample

\"\"

As you can see, it can be many differ

4条回答
  •  抹茶落季
    2021-02-19 01:57

    Numeric values are returned as type :float

    def convert_cell(cell)
      if cell.is_a?(Float)
        i = cell.to_i
        cell == i.to_f ? i : cell
      else
        cell
      end
    end
    
    convert_cell("foobar") # => "foobar"
    convert_cell(123) # => 123
    convert_cell(123.4) # => 123.4
    

提交回复
热议问题