• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Ruby / Ruby Ceiling – Find the Ceiling of a Number with the ceil Method

Ruby Ceiling – Find the Ceiling of a Number with the ceil Method

October 17, 2022 Leave a Comment

We can use Ruby to find the ceiling of a number by using the ceil method. The ceil Ruby method rounds a number up to the nearest integer.

4.653.ceil

The above code would return 5, as it would round the number 4.653 up to 5.

Note that we can also pass the ceil method an integer parameter that will be the number of decimal places that we want to round the number to.

4.653.ceil(2)

Let’s see the output of both of these simple examples.

num1 = 4.653.ceil
num2 = 4.653.ceil(2)

puts num1
puts num2

#Output:
5
4.66

One calculation that is very easy to perform in Ruby is finding the ceiling of a number.

We can find the ceil of a number easily with the Ruby ceil method.

Below are some examples of how to use ceil in Ruby to round up and find the ceiling of numbers.

puts 5.2.ceil
puts 10.867986.ceil(3)
puts 43.5.ceil(1)
puts -12.3.ceil
puts -0.3.ceil

#Output:
6
10.868
43.5
-12
-0.0

If you want to round down and get the floor of a number in Ruby, you can use the floor method. To round to the nearest integer in Ruby, we can use the round method.

Hopefully this article has been beneficial for you to learn how to get the ceiling in Ruby of a number by using the Ruby ceil method.

Other Articles You'll Also Like:

  • 1.  Using Ruby to Create List of Prime Numbers
  • 2.  Using Ruby to Capitalize First Letter of Each Word
  • 3.  Using Ruby to Get the Length of a String
  • 4.  Ruby has_key Method – Check if Hash Has Key
  • 5.  Using Ruby to Capitalize First Letter of String
  • 6.  Ruby Round – Round Number to Nearest Integer with the round Method
  • 7.  Using Ruby to Reverse String
  • 8.  Using Ruby to Get the Length of Array
  • 9.  Ruby has_value Method – Check if Hash Has Value
  • 10.  Ruby unshift Method – Add Items to Start of Array

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy