Class: Numeric
- Inherits:
-
Object
- Object
- Numeric
- Defined in:
- lib/pretty_round.rb
Instance Method Summary collapse
-
#mceil(num) ⇒ Object
Return nearest multiple of given number that is equal to or greater than self.
-
#mfloor(num) ⇒ Object
Return nearest multiple of given number that is equal to or less than self.
-
#mround(num) ⇒ Object
Retuen nearest multiple of given number.
-
#mrounddown(num) ⇒ Object
(also: #mtruncate)
Return nearest multiple of given number that the absolute is equal to or less than self.
-
#mroundup(num) ⇒ Object
Return nearest multiple of given number that the absolute is equal to or greater than self.
-
#rounddown(digit = 0) ⇒ Object
Rounding down with given precision.
-
#roundup(digit = 0) ⇒ Object
Rounding up with given precision.
-
#sceil(digit) ⇒ Object
Ceiling with given significant digit.
-
#sfloor(digit) ⇒ Object
Flooring with given significant digit.
-
#sround(digit) ⇒ Object
Rounding off with given significant digit.
-
#srounddown(digit) ⇒ Object
(also: #struncate)
Rounding down with given significant digit.
-
#sroundup(digit) ⇒ Object
Rounding up with given significant digit.
Instance Method Details
#mceil(num) ⇒ Object
Return nearest multiple of given number that is equal to or greater than self. This method round up to the positive infinity direction.
28 29 30 31 32 33 34 |
# File 'lib/pretty_round.rb', line 28 def mceil(num) if (x = num * div(num)) == self self else [x, x+num].max end end |
#mfloor(num) ⇒ Object
Return nearest multiple of given number that is equal to or less than self. This method round down to the negative infinity direction.
38 39 40 41 42 43 44 |
# File 'lib/pretty_round.rb', line 38 def mfloor(num) if (x = num * div(num)) == self self else [x, x+num].min end end |
#mround(num) ⇒ Object
Retuen nearest multiple of given number. When self is median of multiple of given number, return the multiple that have greater absolute.
68 69 70 71 72 73 74 75 76 |
# File 'lib/pretty_round.rb', line 68 def mround(num) if (x = num * div(num)) == self self elsif x + x +num == self + self # if self is median [x, x+num].max_by(&:abs) else [x, x+num].min_by{|t| (t - self).abs} end end |
#mrounddown(num) ⇒ Object Also known as: mtruncate
Return nearest multiple of given number that the absolute is equal to or less than self. This method round down to near to 0 direction.
58 59 60 61 62 63 64 |
# File 'lib/pretty_round.rb', line 58 def mrounddown(num) if (x = num * div(num)) == self self else [x, x+num].min_by(&:abs) end end |
#mroundup(num) ⇒ Object
Return nearest multiple of given number that the absolute is equal to or greater than self. This method round up to far from 0 direction.
48 49 50 51 52 53 54 |
# File 'lib/pretty_round.rb', line 48 def mroundup(num) if (x = num * div(num)) == self self else [x, x+num].max_by(&:abs) end end |
#rounddown(digit = 0) ⇒ Object
Rounding down with given precision. This method round down to near to 0 direction.
16 17 18 19 20 21 22 23 |
# File 'lib/pretty_round.rb', line 16 def rounddown(digit=0) x = 10**(-digit) if self > 0 quo(x).floor * x else quo(x).ceil * x end end |
#roundup(digit = 0) ⇒ Object
Rounding up with given precision. This method round up to far from 0 direction.
5 6 7 8 9 10 11 12 |
# File 'lib/pretty_round.rb', line 5 def roundup(digit=0) x = 10**(-digit) if self > 0 quo(x).ceil * x else quo(x).floor * x end end |
#sceil(digit) ⇒ Object
Ceiling with given significant digit.
82 83 84 85 |
# File 'lib/pretty_round.rb', line 82 def sceil(digit) selfdigit = Math.log10(abs).floor + 1 mceil(10**(selfdigit - digit)) end |
#sfloor(digit) ⇒ Object
Flooring with given significant digit.
88 89 90 91 |
# File 'lib/pretty_round.rb', line 88 def sfloor(digit) selfdigit = Math.log10(abs).floor + 1 mfloor(10**(selfdigit - digit)) end |
#sround(digit) ⇒ Object
Rounding off with given significant digit.
106 107 108 109 |
# File 'lib/pretty_round.rb', line 106 def sround(digit) selfdigit = Math.log10(abs).floor + 1 mround(10**(selfdigit - digit)) end |
#srounddown(digit) ⇒ Object Also known as: struncate
Rounding down with given significant digit.
100 101 102 103 |
# File 'lib/pretty_round.rb', line 100 def srounddown(digit) selfdigit = Math.log10(abs).floor + 1 mrounddown(10**(selfdigit - digit)) end |
#sroundup(digit) ⇒ Object
Rounding up with given significant digit.
94 95 96 97 |
# File 'lib/pretty_round.rb', line 94 def sroundup(digit) selfdigit = Math.log10(abs).floor + 1 mroundup(10**(selfdigit - digit)) end |