seamusabshere / data_miner

Download, unpack from a ZIP/TAR/GZ/BZ2 archive, parse, correct, convert units and import Google Spreadsheets, XLS, ODS, XML, CSV, HTML, etc. into your ActiveRecord models. Uses RemoteTable gem internally.
MIT License
302 stars 18 forks source link

Deal with numbers that contain commas #15

Closed ihough closed 12 years ago

ihough commented 12 years ago

Currently if a csv contains "1,605.33` this is imported as 1

seamusabshere commented 12 years ago

@ihough i suggest adding (you need all the funky array stuff in order to memoize false)

def number_column?
  return @number_column_query[0] if @number_column_query.is_a?(Array)
  @number_column_query = [model.columns_hash[name.to_s].number?]
  @number_column_query[0]
end

then

if number_column?
  period_position = value.rindex '.'
  comma_position = value.rindex ','
  if period_position and comma_position and comma_position > period_position
    # uncommon euro style 1.000.000,53
    value = value.delete('.').gsub(',', '.')
  elsif comma_position
    # more common 4,000[.00] style - still don't want the commas
    value = value.delete(',')
  end
end