Ruby floor syntax
number.floor
In Ruby, number.floor rounds toward negative infinity. The floor of 2.3 is 2, the floor of −2.3 is −3, and 2 stays 2.
number.floor
puts((2.8).floor)
2
number.floor
| Input | A finite Float receiver in these examples. |
|---|---|
| Result | An Integer when called without a precision argument. Infinity and NaN raise FloatDomainError. |
A test bench increases a reading by one tenth of a unit at a time. Watch where the rounded display changes as the sample passes from one integer toward the next.
class MathExample
def getFloorValue
num1 = (1.0).floor;
num2 = (1.1).floor;
num3 = (1.2).floor;
num4 = (1.3).floor;
num5 = (1.4).floor;
num6 = (1.5).floor;
num7 = (1.6).floor;
num8 = (1.7).floor;
num9 = (1.8).floor;
num10 = (1.9).floor;
num11 = (2.0).floor;
puts("The floor value of 1.0 is " + num1.to_s);
puts("The floor value of 1.1 is " + num2.to_s);
puts("The floor value of 1.2 is " + num3.to_s);
puts("The floor value of 1.3 is " + num4.to_s);
puts("The floor value of 1.4 is " + num5.to_s);
puts("The floor value of 1.5 is " + num6.to_s);
puts("The floor value of 1.6 is " + num7.to_s);
puts("The floor value of 1.7 is " + num8.to_s);
puts("The floor value of 1.8 is " + num9.to_s);
puts("The floor value of 1.9 is " + num10.to_s);
puts("The floor value of 2.0 is " + num11.to_s);
end
end
obj = MathExample.new
obj.getFloorValue
The floor value of 1.0 is 1 The floor value of 1.1 is 1 The floor value of 1.2 is 1 The floor value of 1.3 is 1 The floor value of 1.4 is 1 The floor value of 1.5 is 1 The floor value of 1.6 is 1 The floor value of 1.7 is 1 The floor value of 1.8 is 1 The floor value of 1.9 is 1 The floor value of 2.0 is 2
A scheduling display groups positive measurements into whole-unit intervals. Compare readings just on either side of an integer to see which interval the display would select.
class MathExample
def getFloorValue
num1 = (1.5698).floor;
num2 = (1.3093).floor;
num3 = (1.0087).floor;
num4 = (1.1038).floor;
num5 = (1.9573).floor;
num6 = (1.7462).floor;
num7 = (1.6427).floor;
num8 = (1.4128).floor;
num9 = (1.8099).floor;
num10 = (1.2743).floor;
num11 = (2.0001).floor;
puts("The floor value of 1.5698 is " + num1.to_s);
puts("The floor value of 1.3093 is " + num2.to_s);
puts("The floor value of 1.0087 is " + num3.to_s);
puts("The floor value of 1.1038 is " + num4.to_s);
puts("The floor value of 1.9573 is " + num5.to_s);
puts("The floor value of 1.7462 is " + num6.to_s);
puts("The floor value of 1.6427 is " + num7.to_s);
puts("The floor value of 1.4128 is " + num8.to_s);
puts("The floor value of 1.8099 is " + num9.to_s);
puts("The floor value of 1.2743 is " + num10.to_s);
puts("The floor value of 2.0001 is " + num11.to_s);
end
end
obj = MathExample.new
obj.getFloorValue
The floor value of 1.5698 is 1 The floor value of 1.3093 is 1 The floor value of 1.0087 is 1 The floor value of 1.1038 is 1 The floor value of 1.9573 is 1 The floor value of 1.7462 is 1 The floor value of 1.6427 is 1 The floor value of 1.4128 is 1 The floor value of 1.8099 is 1 The floor value of 1.2743 is 1 The floor value of 2.0001 is 2
A temperature logger records values just below zero or another integer boundary. Follow the rounding direction on the negative side before using the result as the label of a chart interval.
class MathExample
def getFloorValue
num1 = (-1.5698).floor;
num2 = (-1.3093).floor;
num3 = (-1.0087).floor;
num4 = (-1.1038).floor;
num5 = (-1.9573).floor;
num6 = (-1.7462).floor;
num7 = (-1.6427).floor;
num8 = (-1.4128).floor;
num9 = (-1.8099).floor;
num10 = (-1.2743).floor;
num11 = (-2.0001).floor;
puts("The floor value of -1.5698 is " + num1.to_s);
puts("The floor value of -1.3093 is " + num2.to_s);
puts("The floor value of -1.0087 is " + num3.to_s);
puts("The floor value of -1.1038 is " + num4.to_s);
puts("The floor value of -1.9573 is " + num5.to_s);
puts("The floor value of -1.7462 is " + num6.to_s);
puts("The floor value of -1.6427 is " + num7.to_s);
puts("The floor value of -1.4128 is " + num8.to_s);
puts("The floor value of -1.8099 is " + num9.to_s);
puts("The floor value of -1.2743 is " + num10.to_s);
puts("The floor value of -2.0001 is " + num11.to_s);
end
end
obj = MathExample.new
obj.getFloorValue
The floor value of -1.5698 is -2 The floor value of -1.3093 is -2 The floor value of -1.0087 is -2 The floor value of -1.1038 is -2 The floor value of -1.9573 is -2 The floor value of -1.7462 is -2 The floor value of -1.6427 is -2 The floor value of -1.4128 is -2 The floor value of -1.8099 is -2 The floor value of -1.2743 is -2 The floor value of -2.0001 is -3
A sensor can use infinity to mark a reading outside its measuring range. Pass both signs through this operation so the display can handle the result or the reported error deliberately.
class MathExample
def getFloorValue
# Positive ∞ infinite value
n = Float::INFINITY.floor
puts("The floor value of Infinity is " + n.to_s)
# Negative ∞ infinite value
x = (-Float::INFINITY).floor
puts("The floor value of -Infinity is " + x.to_s)
end
end
obj = MathExample.new
obj.getFloorValue
`floor': Infinity (FloatDomainError)
The costume room has 8.5 metres of ribbon left before a performance. Each bag needs one unbroken 1.2-metre strap. The volunteer needs the number of bags she can finish from this roll.
metres, per_strap = 8.5, 1.2
puts "#{(metres / per_strap).floor} straps"
7 straps
Seven straps use 8.4 metres. The last tenth of a metre cannot make another strap.
Reference: Ruby numeric operations