Class: ReeNumber::RoundHelper

Inherits:
Object
  • Object
show all
Includes:
Ree::FnDSL
Defined in:
lib/ree_lib/packages/ree_number/package/ree_number/functions/round_helper.rb

Constant Summary collapse

ROUND_MODES =
[
  :up, # round away from zero
  :down, # round towards zero (truncate)
  :truncate, # round towards zero (truncate)
  :half_up, # round towards the nearest neighbor, unless both neighbors are equidistant, in which case round away from zero. (default)
  :default, # round towards the nearest neighbor, unless both neighbors are equidistant, in which case round away from zero. (default)
  :half_down, # round towards the nearest neighbor, unless both neighbors are equidistant, in which case round towards zero.
  :half_even, # round towards the nearest neighbor, unless both neighbors are equidistant, in which case round towards the even neighbor (Banker's rounding)
  :banker, # round towards the nearest neighbor, unless both neighbors are equidistant, in which case round towards the even neighbor (Banker's rounding)
  :ceiling, # round towards positive infinity (ceil)
  :floor, # round towards negative infinity (floor)
]
DEFAULTS =
{
  precision: 3,
  significant: false,
  round_mode: :default
}.freeze

Instance Method Summary collapse

Instance Method Details

#call(number, **opts) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ree_lib/packages/ree_number/package/ree_number/functions/round_helper.rb', line 37

def call(number, **opts)
  options = DEFAULTS.merge(opts)

  absolute_precision = absolute_precision(
    number, options[:significant], options[:precision]
  )

  rounded_number = convert_to_decimal(number, options[:precision]).round(absolute_precision, options[:round_mode])

  rounded_number = rounded_number.zero? ? rounded_number.abs : rounded_number
end