DEV Community

loading...
Cover image for Numeric data types in Ruby

Numeric data types in Ruby

eminarium profile image Merdan Durdiyev ・5 min read

In Ruby, numbers are used for regular calculations as in all programming languages. In short, all types of numeric data are gathered under "Numeric" data type class. And it has several subclasses under itself.

“Numeric” class has these subclasses:

Numeric

  • Integer (“Fixnum” and “Bignum”)
  • - Fixnum (3, medium size integers/whole numbers)
  • - Bignum (111111111111, huge numbers)
  • Float (5.0, non integer/float numbers with lower precision)
  • Complex (1+3i, complex numbers used in maths)
  • Rational (2/3, to show division relation)
  • BigDecimal (3.0, high precision non integer/float numbers)

You do not have to pay too much attention to these subclasses and their relations unless you want to dive into numeric calculations. In your daily coding operations, you will not even pay attention to these classes. If you are performing too high precision calculations or working with really big numbers, then you can go to the documentation page and read a bit more about Numeric classes.

For instance, imagine you are developing software to calculate a country budget that counts every single penny of expenditures. In this case, you can not go with just the "Float" class but will have to use elements of the “BigDecimal” class for sure.

Let me give one more example to this. Imagine you are developing a POS software that manages daily sales and makes some calculations for daily, monthly, yearly reports of the shop. In this software, I think the "Fixnum" class numbers will do you enough. But if you are developing software for a company that makes nationwide sales and even deals with huge volumes of import and export of various goods, you will surely have to use numbers of “Bignum” class. One more thing you have to consider is that, when you assign a value to a variable, Ruby automatically sets the class of that numeric variable, whether "Fixnum" or "Bignum", based on the value that is being assigned to it. So, you absolutely don't have to worry about that.

You can find out the class of a number or a variable by using the "class" method and outputting that out to the screen.

3.2.class
=> Float
4.class
=> Fixnum
Enter fullscreen mode Exit fullscreen mode

If you want to make that number negative or positive, just put a "-" sign before the value.

Available operators

Now let's take a look at the operators that can be used with numeric values :

  • + (addition)
  • - (subtraction)
  • * (multiplication)
  • / (division)
  • % (finding remainder of division operation)
  • < (less than)
  • <= (less than or equal to)
  • > (greater than)
  • >= (greater than or equal to)
  • == (is equal to, checks whether numbers are equal)
  • ** (power operation, degree of a number)
# The strangest operator here might be the '%'.
# remainder of 5 divided by 2 is => 1
# remainder of 13 divided by 5 is => 3
puts 5%2  # Output => 1
puts 13%5 # Output => 3
# 2 to the power of 3 =>  2^3 = 8
puts 2**3 # Output => 8.
Enter fullscreen mode Exit fullscreen mode

These are methods to convert a numeric value to something else or another type of numeric class:

  • to_i / to_int (converts the value to integer)
  • to_f (converts the value to float value)
  • to_s (converts the numeric value to String value)
price = 4
# We usually use float value for a price, 
# to show cents as well.
# So, let's convert it to "Float" before outputting.
# To output it onto the screen, let's convert it to String

puts "Price of product is = " + price.to_f.to_s
Enter fullscreen mode Exit fullscreen mode

This way, we converted integer value to float and then converted it to String to be able to output it on the screen.

age = 4
# We always use integer for an age, 
# The input is read as a string initially.
# So let's convert input value to "Integer" class.
# To be able to output it onto the screen,
# let's convert it to a String value. 

puts "I am "+ age.to_i.to_s + " years old!"
Enter fullscreen mode Exit fullscreen mode

In this code fragment, we converted a value from string to integer, then from integer to string back again.

One more thing we have to keep in mind is that in division operation details. If both of the values used in division (divisor or quotient) are "Integer" class values, then the result is also an Integer. If one of the values in the division is a "Float" class instance, then the result will also be a "Float" type value. For instance:

# Both of the values are integer.
puts 7/2  # Output is 3. You might have expected 3.5
# Only one of the values is an integer.
puts 7.0/2  # Output is => 3.5
puts 7/2.0  # Output is => 3.5
# Let's use "to_f" function.
puts 7/2.to_f  # Output is => 3.5
Enter fullscreen mode Exit fullscreen mode

Ruby has much more comforting tools. If you want to insert "thousands" value divisors in the number value, you can do that. This way, the number is read easily. Ruby automatically converts it to a corresponding number value.

# Outputting a huge numer.
puts 5_300 # Output => 5300
puts 3_000_950  # Output => 3000950.
Enter fullscreen mode Exit fullscreen mode

This might not be a huge advantage but it gives some interesting tricks to the user and so attracts more new learners of the programming language.

Built-in Methods (Numeric class)

Now, let's look at the built-in spectacular methods that we can use with numeric values.

Even

“.even?” method checks whether the value we are working on is an even number or not. It returns “true” if it is and “false” if it is not.

15.even? #=> false
4.even?  #=> true
Enter fullscreen mode Exit fullscreen mode

Odd

“.odd?” method checks whether the value we are working on is an odd number or not. It returns “true” if it is and “false” if it is not.

15.odd? #=> true
4.odd?  #=> false
Enter fullscreen mode Exit fullscreen mode

Ceil

“.ceil” method rounds the “Float” type numeric values to the closest integer more than the value itself.

8.3.ceil #=> 9
6.7.ceil #=> 7
Enter fullscreen mode Exit fullscreen mode

Floor

“.floor” method rounds the “Float” type numeric values to the closest integer less than the value itself.

8.3.floor #=> 8
6.7.floor #=> 6
Enter fullscreen mode Exit fullscreen mode

Pred

“.pred” method returns an integer that is one number less than the actual value we are applying it to.

15.pred #=> 14
2.pred  #=> 1
(-4).pred #=> -5
Enter fullscreen mode Exit fullscreen mode

Next

“.next” method returns an integer that is one number more than the actual value we are applying it to.

15.next #=> 16
2.next  #=> 3
-4.next #=> -3
Enter fullscreen mode Exit fullscreen mode

Conclusion

Alt Text

And we came to the end of the article about Ruby numbers. If you have something to add or have spotted some mistakes, just let me know about it in the comments.
Do not hesitate to write any feedback about the article. I will be very glad to hear about your thoughts and suggestions.
Thank you for the time you took to read or run through the article. I hope it was worth it. Good luck!

Discussion (0)

pic
Editor guide