Module: CMDx::Coercions::BigDecimal

Extended by:
BigDecimal
Included in:
BigDecimal
Defined in:
lib/cmdx/coercions/big_decimal.rb

Overview

Converts various input types to BigDecimal format

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

Constant Summary collapse

DEFAULT_PRECISION =
14

Instance Method Summary collapse

Instance Method Details

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

Converts a value to a BigDecimal

Examples:

Convert numeric strings to BigDecimal

BigDecimal.call("123.45")                   # => #<BigDecimal:7f8b8c0d8e0f '0.12345E3',9(18)>
BigDecimal.call("0.001", precision: 6)      # => #<BigDecimal:7f8b8c0d8e0f '0.1E-2',9(18)>

Convert other numeric types

BigDecimal.call(42)                         # => #<BigDecimal:7f8b8c0d8e0f '0.42E2',9(18)>
BigDecimal.call(3.14159)                    # => #<BigDecimal:7f8b8c0d8e0f '0.314159E1',9(18)>

Parameters:

  • value (Object)

    The value to convert to BigDecimal

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

    Optional configuration parameters

Options Hash (options):

  • :precision (Integer)

    The precision to use (defaults to DEFAULT_PRECISION)

Returns:

Raises:

  • (CoercionError)

    If the value cannot be converted to BigDecimal



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

def call(value, options = {})
  BigDecimal(value, options[:precision] || DEFAULT_PRECISION)
rescue ArgumentError, TypeError
  type = Locale.t("cmdx.types.big_decimal")
  raise CoercionError, Locale.t("cmdx.coercions.into_a", type:)
end