module PrettyRound
Public Instance Methods
Return nearest multiple of given number that is equal to or greater than self. This method round up to the positive infinity direction.
# 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
Return nearest multiple of given number that is equal to or less than self. This method round down to the negative infinity direction.
# 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
Retuen nearest multiple of given number. When self is median of multiple of given number, return the multiple that have greater absolute.
# 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
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.
# 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
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.
# 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
Rounding down with given precision. This method round down to near to 0 direction.
# File lib/pretty_round/core.rb, line 11 def rounddown(digit=0) abs.floor(digit) * (positive? ? 1 : -1) end
Rounding up with given precision. This method round up to far from 0 direction.
# File lib/pretty_round/core.rb, line 5 def roundup(digit=0) abs.ceil(digit) * (positive? ? 1 : -1) end
Ceiling with given significant digit.
# 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
Flooring with given significant digit.
# 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
Rounding off with given significant digit.
# 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
Rounding down with given significant digit.
# 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
Rounding up with given significant digit.
# 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