Class: CSVConverter::Converters::FloatConverter

Inherits:
BaseConverter show all
Defined in:
lib/csv_converter/converters/float_converter.rb

Overview

Converts a string into a float

Instance Attribute Summary

Attributes inherited from BaseConverter

#options, #raw_data

Instance Method Summary collapse

Methods inherited from BaseConverter

#data, #empty_value?, #initialize

Constructor Details

This class inherits a constructor from CSVConverter::Converters::BaseConverter

Instance Method Details

#callFloat

Converts data into a Float. If the decimal separator is a comma it is replaced by a period before parsing.

Returns:

  • (Float)

    if an error occurs during conversion nil is returned.



10
11
12
13
14
# File 'lib/csv_converter/converters/float_converter.rb', line 10

def call
  call!
rescue CSVConverter::Error
  nullable_object
end

#call!Float

Converts data into a Float. If the decimal separator is a comma it is replaced by a period before parsing.

Returns:

  • (Float)

    if an error occurs during conversion an error is raised.



19
20
21
22
23
# File 'lib/csv_converter/converters/float_converter.rb', line 19

def call!
  Float(data.sub(',', '.'))
rescue StandardError => e
  raise CSVConverter::Error.new(e.message, options)
end