class Float

Float extensions to calculate round numbers.

Public Instance Methods

ceil_at( d ) click to toggle source

Ceil to the given decimal position.

4.555.ceil_at(0)  #=> 5.0
4.555.ceil_at(1)  #=> 4.6
4.555.ceil_at(2)  #=> 4.56
4.555.ceil_at(3)  #=> 4.555

CREDIT: Trans & Joel Parker Henderson

# File lib/sixarm_ruby_numeric_round/float.rb, line 16
def ceil_at( d ) #d=0
  (self * (10 ** d)).ceil.fdiv (10 ** d)
end
ceil_to( n ) click to toggle source

Ceils to the nearest _n_th degree.

4.555.ceil_to(1)     #=> 5.0
4.555.ceil_to(0.1)   #=> 4.6
4.555.ceil_to(0.01)  #=> 4.56
4.555.ceil_to(0)     #=> 4.555

CREDIT: Trans & Joel Parker Henderson

# File lib/sixarm_ruby_numeric_round/float.rb, line 29
def ceil_to( n ) #n=1
  return self if n == 0
  (self * (1.0 / n)).ceil.fdiv (1.0 / n)
end
floor_at( d ) click to toggle source

Floor to the given decimal position.

4.555.floor_at(0)  #=> 4.0
4.555.floor_at(1)  #=> 4.5
4.555.floor_at(2)  #=> 4.55
4.555.floor_at(3)  #=> 4.555

CREDIT: Trans & Joel Parker Henderson

# File lib/sixarm_ruby_numeric_round/float.rb, line 43
def floor_at( d ) #d=0
  (self * (10 ** d)).floor.fdiv (10 ** d)
end
floor_to( n ) click to toggle source

Floors to the nearest _n_th degree.

4.555.floor_to(1)     #=> 4.0
4.555.floor_to(0.1)   #=> 4.5
4.555.floor_to(0.01)  #=> 4.55
4.555.floor_to(0)     #=> 4.555

CREDIT: Trans & Joel Parker Henderson

# File lib/sixarm_ruby_numeric_round/float.rb, line 56
def floor_to( n ) #n=1
  return self if n == 0
  (self * (1.0 / n)).floor.fdiv (1.0 / n)
end
round_at( d ) click to toggle source

Round to the given decimal position.

4.555.round_at(0)  #=> 5.0
4.555.round_at(1)  #=> 4.6
4.555.round_at(2)  #=> 4.56
4.555.round_at(3)  #=> 4.555

CREDIT: Trans & Joel Parker Henderson

# File lib/sixarm_ruby_numeric_round/float.rb, line 70
def round_at( d ) #d=0
  (self * (10 ** d)).round.fdiv (10 ** d)
end
round_to( n ) click to toggle source

Rounds to the nearest _n_th degree.

4.555.round_to(1)     #=> 5
4.555.round_to(0.1)   #=> 4.6
4.555.round_to(0.01)  #=> 4.56
4.555.round_to(0)     #=> 4.555

CREDIT: Trans & Joel Parker Henderson

# File lib/sixarm_ruby_numeric_round/float.rb, line 83
def round_to( n ) #n=1
  return self if n == 0
  (self * (1.0 / n)).round.fdiv (1.0 / n)
end
truncate_at( d ) click to toggle source

Truncate to the given decimal position.

4.555.truncate_at(0)  #=> 4.0
4.555.truncate_at(1)  #=> 4.5
4.555.truncate_at(2)  #=> 4.55
4.555.truncate_at(3)  #=> 4.555

CREDIT: Trans & Joel Parker Henderson

# File lib/sixarm_ruby_numeric_round/float.rb, line 97
def truncate_at( d ) #d=0
  (self * (10 ** d)).truncate.fdiv (10 ** d)
end
truncate_to( n ) click to toggle source

Truncates to the nearest _n_th degree.

4.555.truncate_to(1)     #=> 4.0
4.555.truncate_to(0.1)   #=> 4.5
4.555.truncate_to(0.01)  #=> 4.55
4.555.truncate_to(0)     #=> 4.555

CREDIT: Trans & Joel Parker Henderson

# File lib/sixarm_ruby_numeric_round/float.rb, line 110
def truncate_to( n ) #n=1
  return self if n == 0
  (self * (1.0 / n)).truncate.fdiv (1.0 / n)
end