• 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 Floor – Find Floor of Number with floor Method

Ruby Floor – Find Floor of Number with floor Method

October 17, 2022 Leave a Comment

We can use Ruby to find the floor of a number by using the floor method. The floor Ruby method rounds a number down to the nearest integer.

4.653.floor

The above code would return 4, as it would round the number 4.653 down to 4.

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

4.653.floor(2)

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

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

puts num1
puts num2

#Output:
4
4.65

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

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

Below are some examples of how to use floor in Ruby to round down and find the floor of numbers.

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

#Output:
5
10.867
43.5
-13
-1

If you want to round up and get the ceiling of a number in Ruby, you can use the ceil 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 floor in Ruby of a number by using the Ruby floor method.

Other Articles You'll Also Like:

  • 1.  Ruby unshift Method – Add Items to Start of Array
  • 2.  Using Ruby to Create List of Prime Numbers
  • 3.  Ruby Round – Round Number to Nearest Integer with the round Method
  • 4.  Using Ruby to Print to Console
  • 5.  How to Square a Number in Ruby
  • 6.  How to Print Without Newline in Ruby
  • 7.  Using Ruby to Convert String to Array
  • 8.  Using Ruby to Capitalize First Letter of Each Word
  • 9.  Using Ruby to Convert Array to String
  • 10.  Ruby has_value Method – Check if Hash Has Value

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