Ruby floor - Syntax
int floor(mixed n);
The floor() function in Ruby is used to return the next lowest integer value (i.e. last largest integer not greaterthan the inputed value) by rounding up value if necessary.
int floor(mixed n);
mixed n |
Specifies the real number (int, long, float, double) (*Required) |
---|---|
Return mixed |
Returns a number respective to an input argument. |
Round the floating point number from 1.0 to 2.0 step by 0.1 using floor math method and print the resultant in console.
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
Round the positive floating point number (random numbers) using floor math method and print the resultant in console.
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
Round the negative floating point number (random numbers) using floor math method and print the resultant in console.
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
Try to round the infinity using floor math method, but it will throw an FloatDomainError error.
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