3 ways to make big numbers readable in Ruby

Nov 10, 2021·

1 min read

Today, I came across the following method in my old code. (This comes from an application supporting calculations in the construction of concrete floors).

  def l_factor
    ((bending_stiffness * 10000000000) / k) ** 0.25

This big number looks so unreadable. I started moving the mouse cursor and counting zeros. There are ten of them. I was wondering how I could write it better. And actually, I found 3 ways to make it more readable:

🔍 Underscore notation


You can format a number with grouped thousands using an underscore delimiter. Ruby lets you insert underscores anywhere inside integers.

🧮 Expotential notation


You would just read it like: \( 10^{10}\)

🧑‍🔬 Scientific notation


This one is my best choice. It actually means: \( 1\cdot10^{10}\) and this notation is commonly used by scientists, mathematicians, and engineers.

Which one would you choose? 🤷 Leave a comment.