module PrettyRound

Public Instance Methods

mceil(num) click to toggle source

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
mfloor(num) click to toggle source

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
mround(num) click to toggle source

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
mrounddown(num) click to toggle source

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
Also aliased as: mtruncate
mroundup(num) click to toggle source

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
mtruncate(num)
Alias for: mrounddown
rounddown(digit=0) click to toggle source

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
roundup(digit=0) click to toggle source

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
sceil(digit) click to toggle source

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
sfloor(digit) click to toggle source

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
sround(digit) click to toggle source

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
srounddown(digit) click to toggle source

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
Also aliased as: struncate
sroundup(digit) click to toggle source

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
struncate(digit)
Alias for: srounddown