Created
July 30, 2022 09:06
-
-
Save vKxni/b8c05ada89188817cc14f1f805936133 to your computer and use it in GitHub Desktop.
Format big numbers with Elixir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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