Class: Float

Inherits:
Object
  • Object
show all
Defined in:
lib/sixarm_ruby_numeric_round/float.rb

Overview

Float extensions to calculate round numbers.

Instance Method Summary collapse

Instance Method Details

#ceil_at(d) ⇒ Object

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



17
18
19
# File 'lib/sixarm_ruby_numeric_round/float.rb', line 17

def ceil_at( d ) #d=0
  (self * (10 ** d)).ceil.fdiv (10 ** d)
end

#ceil_to(n) ⇒ Object

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



30
31
32
33
# File 'lib/sixarm_ruby_numeric_round/float.rb', line 30

def ceil_to( n ) #n=1
  return self if n == 0
  (self * (1.0 / n)).ceil.fdiv (1.0 / n)
end

#floor_at(d) ⇒ Object

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



44
45
46
# File 'lib/sixarm_ruby_numeric_round/float.rb', line 44

def floor_at( d ) #d=0
  (self * (10 ** d)).floor.fdiv (10 ** d)
end

#floor_to(n) ⇒ Object

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



57
58
59
60
# File 'lib/sixarm_ruby_numeric_round/float.rb', line 57

def floor_to( n ) #n=1
  return self if n == 0
  (self * (1.0 / n)).floor.fdiv (1.0 / n)
end

#round_at(d) ⇒ Object

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



71
72
73
# File 'lib/sixarm_ruby_numeric_round/float.rb', line 71

def round_at( d ) #d=0
  (self * (10 ** d)).round.fdiv (10 ** d)
end

#round_to(n) ⇒ Object

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



84
85
86
87
# File 'lib/sixarm_ruby_numeric_round/float.rb', line 84

def round_to( n ) #n=1
  return self if n == 0
  (self * (1.0 / n)).round.fdiv (1.0 / n)
end

#truncate_at(d) ⇒ Object

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



98
99
100
# File 'lib/sixarm_ruby_numeric_round/float.rb', line 98

def truncate_at( d ) #d=0
  (self * (10 ** d)).truncate.fdiv (10 ** d)
end

#truncate_to(n) ⇒ Object

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



111
112
113
114
# File 'lib/sixarm_ruby_numeric_round/float.rb', line 111

def truncate_to( n ) #n=1
  return self if n == 0
  (self * (1.0 / n)).truncate.fdiv (1.0 / n)
end