Skip to content

Instantly share code, notes, and snippets.

@vKxni
Created July 30, 2022 09:06
Show Gist options
  • Save vKxni/b8c05ada89188817cc14f1f805936133 to your computer and use it in GitHub Desktop.
Save vKxni/b8c05ada89188817cc14f1f805936133 to your computer and use it in GitHub Desktop.
Format big numbers with Elixir
defmodule Number do
def format(input, separator \\ ",") do
prefix_size = rem(byte_size(input), 3)
{prefix, rest} = String.split_at(input, prefix_size)
segments = for <<chunk::binary-size(3) <- rest>>, do: chunk
Enum.join([prefix | segments], separator)
end
end
# "2342346546" => "2.342.346.546"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment