Class: NumberMuncher::Numeric

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/number_muncher/numeric.rb

Constant Summary collapse

ARITHMETIC_OPERATORS =
%i(+ - * / **).freeze

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Numeric

Returns a new instance of Numeric.



7
8
9
# File 'lib/number_muncher/numeric.rb', line 7

def initialize(value)
  super(parse(value))
end

Instance Method Details

#round(to = nil) ⇒ Object

Raises:



21
22
23
24
25
26
27
28
# File 'lib/number_muncher/numeric.rb', line 21

def round(to = nil)
  return self unless to

  to = Numeric.new(to)
  raise IllegalRoundValue, 'cannot round to nearest 0' if to.zero?

  to * (self / to).to_f.round
end

#to_fraction(**opts) ⇒ Object



30
31
32
# File 'lib/number_muncher/numeric.rb', line 30

def to_fraction(**opts)
  ToFraction.new(opts).call(self)
end

#to_rObject



11
12
13
# File 'lib/number_muncher/numeric.rb', line 11

def to_r
  rational
end