0Pricing
Ruby Academy · Lesson

Rational and BigDecimal

Exact arithmetic.

Why Exact Arithmetic?

Floats lose precision. For money and fractions Ruby offers Rational and BigDecimal that stay exact.

puts 0.1 + 0.2

Creating a Rational

A Rational is an exact fraction. Write it with r suffix or Rational(num, den).

puts (1r / 3)
puts Rational(2, 4)

All lessons in this course

  1. Integers and Floats
  2. Arithmetic and Operators
  3. The Math Module
  4. Rational and BigDecimal
← Back to Ruby Academy