In Elixir, how do you format numbers with string interpolation

Matt picture Matt · Dec 18, 2015 · Viewed 17.1k times · Source

I want to print out a string like

IO.puts("Count: #{my_count}")

But I want leading zeroes in the output like

Count: 006

How do I do that and where is that documentation?

Answer

Dmitry Biletskyy picture Dmitry Biletskyy · Dec 18, 2015

You can use String.pad_leading/3

my_count
|> Integer.to_string
|> String.pad_leading(3, "0")