Module: PrettyRound
- Defined in:
- lib/pretty_round/core.rb,
lib/pretty_round/core.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.
18 19 20 21 22 23 24 |
# File 'lib/pretty_round/core.rb', line 18 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.
28 29 30 31 32 33 34 |
# File 'lib/pretty_round/core.rb', line 28 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.
58 59 60 61 62 63 64 65 66 |
# File 'lib/pretty_round/core.rb', line 58 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.
48 49 50 51 52 53 54 |
# File 'lib/pretty_round/core.rb', line 48 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.
38 39 40 41 42 43 44 |
# File 'lib/pretty_round/core.rb', line 38 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.
11 12 13 |
# File 'lib/pretty_round/core.rb', line 11 def rounddown(digit=0) abs.floor(digit) * (positive? ? 1 : -1) end |
#roundup(digit = 0) ⇒ Object
Rounding up with given precision. This method round up to far from 0 direction.
5 6 7 |
# File 'lib/pretty_round/core.rb', line 5 def roundup(digit=0) abs.ceil(digit) * (positive? ? 1 : -1) end |
#sceil(digit) ⇒ Object
Ceiling with given significant digit.
72 73 74 75 76 |
# File 'lib/pretty_round/core.rb', line 72 def sceil(digit) return self if zero? selfdigit = Math.log10(abs).floor + 1 ceil(digit - selfdigit) end |
#sfloor(digit) ⇒ Object
Flooring with given significant digit.
79 80 81 82 83 |
# File 'lib/pretty_round/core.rb', line 79 def sfloor(digit) return self if zero? selfdigit = Math.log10(abs).floor + 1 floor(digit - selfdigit) end |
#sround(digit) ⇒ Object
Rounding off with given significant digit.
100 101 102 103 104 |
# File 'lib/pretty_round/core.rb', line 100 def sround(digit) return self if zero? selfdigit = Math.log10(abs).floor + 1 round(digit - selfdigit) end |
#srounddown(digit) ⇒ Object Also known as: struncate
Rounding down with given significant digit.
93 94 95 96 97 |
# File 'lib/pretty_round/core.rb', line 93 def srounddown(digit) return self if zero? selfdigit = Math.log10(abs).floor + 1 abs.floor(digit - selfdigit) * (positive? ? 1 : -1) end |
#sroundup(digit) ⇒ Object
Rounding up with given significant digit.
86 87 88 89 90 |
# File 'lib/pretty_round/core.rb', line 86 def sroundup(digit) return self if zero? selfdigit = Math.log10(abs).floor + 1 abs.ceil(digit - selfdigit) * (positive? ? 1 : -1) end |