Ruby ceil - Syntax
mixed ceil(mixed n);
The ceil() function in Ruby is used to return the next highest integer value by rounding up value if necessary.
mixed ceil(mixed n);
mixed n |
Specifies the real number (int, long, float, double) (*Required) |
---|---|
Return int |
Returns an integer respective to an input argument. |
Round the floating point number from 1.0 to 2.0 step by 0.1 using ceiling math method and print the resultant in console.
class MathExample
def getCeilValue
num1 = (1.0).ceil;
num2 = (1.1).ceil;
num3 = (1.2).ceil;
num4 = (1.3).ceil;
num5 = (1.4).ceil;
num6 = (1.5).ceil;
num7 = (1.6).ceil;
num8 = (1.7).ceil;
num9 = (1.8).ceil;
num10 = (1.9).ceil;
num11 = (2.0).ceil;
puts("The ceiling value of 1.0 is " + num1.to_s);
puts("The ceiling value of 1.1 is " + num2.to_s);
puts("The ceiling value of 1.2 is " + num3.to_s);
puts("The ceiling value of 1.3 is " + num4.to_s);
puts("The ceiling value of 1.4 is " + num5.to_s);
puts("The ceiling value of 1.5 is " + num6.to_s);
puts("The ceiling value of 1.6 is " + num7.to_s);
puts("The ceiling value of 1.7 is " + num8.to_s);
puts("The ceiling value of 1.8 is " + num9.to_s);
puts("The ceiling value of 1.9 is " + num10.to_s);
puts("The ceiling value of 2.0 is " + num11.to_s);
end
end
obj = MathExample.new
obj.getCeilValue
Round the positive floating point number (random numbers) using ceiling math method and print the resultant in console.
class MathExample
def getCeilValue
num1 = (1.5698).ceil;
num2 = (1.3093).ceil;
num3 = (1.0087).ceil;
num4 = (1.1038).ceil;
num5 = (1.9573).ceil;
num6 = (1.7462).ceil;
num7 = (1.6427).ceil;
num8 = (1.4128).ceil;
num9 = (1.8099).ceil;
num10 = (1.2743).ceil;
num11 = (2.0001).ceil;
puts("The ceiling value of 1.5698 is " + num1.to_s);
puts("The ceiling value of 1.3093 is " + num2.to_s);
puts("The ceiling value of 1.0087 is " + num3.to_s);
puts("The ceiling value of 1.1038 is " + num4.to_s);
puts("The ceiling value of 1.9573 is " + num5.to_s);
puts("The ceiling value of 1.7462 is " + num6.to_s);
puts("The ceiling value of 1.6427 is " + num7.to_s);
puts("The ceiling value of 1.4128 is " + num8.to_s);
puts("The ceiling value of 1.8099 is " + num9.to_s);
puts("The ceiling value of 1.2743 is " + num10.to_s);
puts("The ceiling value of 2.0001 is " + num11.to_s);
end
end
obj = MathExample.new
obj.getCeilValue
Round the negative floating point number (random numbers) using ceiling math method and print the resultant in console.
class MathExample
def getCeilValue
num1 = (-1.5698).ceil;
num2 = (-1.3093).ceil;
num3 = (-1.0087).ceil;
num4 = (-1.1038).ceil;
num5 = (-1.9573).ceil;
num6 = (-1.7462).ceil;
num7 = (-1.6427).ceil;
num8 = (-1.4128).ceil;
num9 = (-1.8099).ceil;
num10 = (-1.2743).ceil;
num11 = (-2.0001).ceil;
puts("The ceiling value of -1.5698 is " + num1.to_s);
puts("The ceiling value of -1.3093 is " + num2.to_s);
puts("The ceiling value of -1.0087 is " + num3.to_s);
puts("The ceiling value of -1.1038 is " + num4.to_s);
puts("The ceiling value of -1.9573 is " + num5.to_s);
puts("The ceiling value of -1.7462 is " + num6.to_s);
puts("The ceiling value of -1.6427 is " + num7.to_s);
puts("The ceiling value of -1.4128 is " + num8.to_s);
puts("The ceiling value of -1.8099 is " + num9.to_s);
puts("The ceiling value of -1.2743 is " + num10.to_s);
puts("The ceiling value of -2.0001 is " + num11.to_s);
end
end
obj = MathExample.new
obj.getCeilValue
Try to round the infinity using ceiling math method, but it will throw an FloatDomainError error.
class MathExample
def getCeilValue
# Positive ∞ infinite value
n = Float::INFINITY.ceil
puts("The ceiling value of Infinity is " + n.to_s)
# Negative ∞ infinite value
x = (-Float::INFINITY).ceil
puts("The ceiling value of -Infinity is " + x.to_s)
end
end
obj = MathExample.new
obj.getCeilValue