Fractional Numbers
Explore the concept of fractional numbers in Ruby and understand the differences between float and BigDecimal types. Learn why floating point representation is used, how Ruby converts types, and when to choose precise calculations with BigDecimal versus faster inexact calculations with floats.
We'll cover the following...
Consider some popular type castings that we’re already familiar with. One or another object may have one or more of the following methods:
-
.to_i: This converts something to an integer. For example, it converts a string to a number. -
.to_s: This converts something to a string. For example, it converts a number to a string. -
.to_f: This converts something to a fraction. For example, it converts a string to a fraction.
Float type
But… wait! It’s float, not fraction! Why is that? In computer literature, the term “floating point” is derived from the fact that there is no fixed number of ...