Module: Hesabu::Types

Defined in:
lib/hesabu/types/numeric.rb

Constant Summary collapse

MAXDECIMAL =
Float::DIG + 1

Class Method Summary collapse

Class Method Details

.as_bigdecimal(number) ⇒ Object



29
30
31
# File 'lib/hesabu/types/numeric.rb', line 29

def self.as_bigdecimal(number)
  BigDecimal(number, MAXDECIMAL)
end

.as_numeric(value) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/hesabu/types/numeric.rb', line 12

def self.as_numeric(value)
  if value.is_a?(::Numeric)
    return value.to_i if value.to_i == value
    return value
  end
  if value.is_a?(::String)
    number = value[/\A-?\d*\.?\d+\z/]
    if number
      if number.include?(".")
        return BigDecimal(number, MAXDECIMAL)
      else
        return number.to_i
      end
    end
  end
end

.as_numeric!(value) ⇒ Object



6
7
8
9
10
# File 'lib/hesabu/types/numeric.rb', line 6

def self.as_numeric!(value)
  numeric = as_numeric(value)
  return numeric if numeric
  raise "Not a numeric : '#{value}' (#{value.class.name})"
end