Class: Numeric

Inherits:
Object show all
Defined in:
lib/inactive_support/numeric/precision.rb

Overview

A more precise rounding. ~4 times slower than simple round

3.904605.round(2)
# => 3.9
3.904605.precision(2)
# => 3.91

37.9945.round(2)
# => 37.99
37.9945.precision(2)
# => 38.0

Instance Method Summary collapse

Instance Method Details

#precision(precision = 0) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/inactive_support/numeric/precision.rb', line 14

def precision(precision = 0)
  power = 10**precision

  if precision == 0
    round
  else
    powered = self * power

    (precision - 1).downto(0).each do |i|
      powered = powered.round(i).to_f
    end

    powered.to_f / power.to_f
  end
end