Class: Numeric
Overview
Numeric extensions
Instance Method Summary collapse
-
#floor_precision(precision) ⇒ Object
Self, truncated to a given precision.
-
#if(flag) ⇒ Object
0 if the given flag is any of: nil, false, 0, [], {}.
-
#percent(ndigits = 0) ⇒ Object
Self as a percent, i.e.
-
#unless(flag) ⇒ Object
Self if flag is nil, false, 0, [], {}; otherwise return 0.
Instance Method Details
#floor_precision(precision) ⇒ Object
Returns self, truncated to a given precision.
53 54 55 56 |
# File 'lib/sixarm_ruby_ramp/numeric.rb', line 53 def floor_precision(precision) return self if is_a?(Float) && nan? (self * (10 ** precision)).truncate.fdiv(10 ** precision) end |
#if(flag) ⇒ Object
Returns 0 if the given flag is any of: nil, false, 0, [], {}.
14 15 16 |
# File 'lib/sixarm_ruby_ramp/numeric.rb', line 14 def if(flag) if [nil,false,0,[],{}].include?(flag) then 0 else self end end |
#percent(ndigits = 0) ⇒ Object
Returns self as a percent, i.e. * 100 then call round.
39 40 41 |
# File 'lib/sixarm_ruby_ramp/numeric.rb', line 39 def percent(ndigits=0) (self * 100).round(ndigits) end |
#unless(flag) ⇒ Object
Returns self if flag is nil, false, 0, [], {}; otherwise return 0.
25 26 27 |
# File 'lib/sixarm_ruby_ramp/numeric.rb', line 25 def unless(flag) if [nil,false,0,[],{}].include?(flag) then self else 0 end end |