Module: CMDx::Coercions::Float

Extended by:
Float
Included in:
Float
Defined in:
lib/cmdx/coercions/float.rb

Overview

Converts various input types to Float format

Handles conversion from numeric strings, integers, and other numeric types that can be converted to floats using Ruby’s Float() method.

Instance Method Summary collapse

Instance Method Details

#call(value, options = {}) ⇒ Float

Converts a value to a Float

Examples:

Convert numeric strings to float

Float.call("123")        # => 123.0
Float.call("123.456")    # => 123.456
Float.call("-42.5")      # => -42.5
Float.call("1.23e4")     # => 12300.0

Convert numeric types to float

Float.call(42)           # => 42.0
Float.call(BigDecimal("123.456")) # => 123.456
Float.call(Rational(3, 4))       # => 0.75
Float.call(Complex(5.0, 0))      # => 5.0

Parameters:

  • value (Object)

    The value to convert to a float

  • options (Hash) (defaults to: {})

    Optional configuration parameters (currently unused)

Options Hash (options):

  • :unused (Object)

    Currently no options are used

Returns:

  • (Float)

    The converted float value

Raises:

  • (CoercionError)

    If the value cannot be converted to a float



35
36
37
38
39
40
# File 'lib/cmdx/coercions/float.rb', line 35

def call(value, options = {})
  Float(value)
rescue ArgumentError, RangeError, TypeError
  type = Locale.t("cmdx.types.float")
  raise CoercionError, Locale.t("cmdx.coercions.into_a", type:)
end