Follow

Follow

3 ways to make big numbers readable in Ruby

Piotr Jurewicz's photo
Piotr Jurewicz
·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
  end

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

10_000_000_000

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

🧮 Expotential notation

10**10

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

🧑‍🔬 Scientific notation

1e10

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.

 
Share this