3 ways to make big numbers readable in Ruby

ยท

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.