Created
August 25, 2015 09:07
-
-
Save smeevil/f792dd92255292b909e9 to your computer and use it in GitHub Desktop.
price formatter
This file contains hidden or 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 Sheep.PriceFormatter do | |
def format(number) when is_integer(number) do | |
"€ " <> (number |> set_decimal |> Float.to_string(decimals: 2) |> set_separators) | |
end | |
def format(not_a_number) do | |
string_number = not_a_number |> to_string |> String.replace(~r/\D/, "") | |
{number, _} = Integer.parse(string_number) | |
number |> format | |
end | |
def amount_to_cents(amount) do | |
case Integer.parse(amount) do | |
{number,""} -> number * 100 | |
{_,_} -> | |
{number, _} = amount |> String.replace(~r/\D/, "") |> Integer.parse | |
number | |
end | |
end | |
defp set_decimal(number) do | |
number/100.0 | |
end | |
defp set_separators(string) do | |
[number, fraction] = string |> String.split(".") | |
format_number(number) <> "," <> format_fraction(fraction) | |
end | |
defp format_number(number) do | |
number #1234567 | |
|> to_char_list #[1,2,3,4,5,6,7] | |
|> :lists.reverse #[7,6,5,4,3,2,1] | |
|> delimit_number #[7,6,5,.,4,3,2,.,1] | |
|> :lists.reverse #[1,.,2,3,4,.,5,6,7]] | |
|> to_string #1.234.567 | |
end | |
defp delimit_number(number, formatted \\ '') | |
defp delimit_number(number, formatted) when length(number) <= 3 do | |
formatted ++ number | |
end | |
defp delimit_number([a,b,c | tail], formatted) do | |
tail |> delimit_number(formatted ++ [a,b,c, '.']) | |
end | |
defp format_fraction("0"), do: "-" | |
defp format_fraction("00"), do: "-" | |
defp format_fraction(fraction) when byte_size(fraction) == 1 , do: "#{fraction}0" | |
defp format_fraction(fraction), do: fraction | |
end |
This file contains hidden or 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 Sheep.PriceFormatterTest do | |
use ExUnit.Case, async: true | |
alias Sheep.PriceFormatter | |
test "should format an amount_in_cents as integer to a price" do | |
assert "€ 0,01" = PriceFormatter.format(1) | |
assert "€ 0,12" = PriceFormatter.format(12) | |
assert "€ 1,23" = PriceFormatter.format(123) | |
assert "€ 12,34" = PriceFormatter.format(1234) | |
assert "€ 123,45" = PriceFormatter.format(12345) | |
assert "€ 1.234,56" = PriceFormatter.format(123456) | |
assert "€ 1.000,-" = PriceFormatter.format(100000) | |
assert "€ 12.345,67" = PriceFormatter.format(1234567) | |
assert "€ 123.456,78" = PriceFormatter.format(12345678) | |
assert "€ 1.234.567,89" = PriceFormatter.format(123456789) | |
assert "€ 12.345.678,90" = PriceFormatter.format(1234567890) | |
assert "€ 1,-" = PriceFormatter.format(100) | |
end | |
test "should format an amount_in_cents as string to a price" do | |
assert "€ 0,01" = PriceFormatter.format("1") | |
assert "€ 0,12" = PriceFormatter.format("12") | |
assert "€ 1,23" = PriceFormatter.format("12.3") | |
assert "€ 12,34" = PriceFormatter.format("12.34") | |
assert "€ 123,45" = PriceFormatter.format("123,45") | |
assert "€ 1.234,56" = PriceFormatter.format("1234,56") | |
assert "€ 12.345,67" = PriceFormatter.format("12345.67") | |
assert "€ 123.456,78" = PriceFormatter.format("123456,78") | |
assert "€ 1.234.567,89" = PriceFormatter.format("1234567.89") | |
assert "€ 12.345.678,90" = PriceFormatter.format("12345678,90") | |
assert "€ 1,-" = PriceFormatter.format("1.00") | |
assert "€ 1,-" = PriceFormatter.format("1,00") | |
end | |
test "should convert amount to amount in cents" do | |
assert 123456 = PriceFormatter.amount_to_cents("1234,56") | |
assert 123456 = PriceFormatter.amount_to_cents("1234.56") | |
assert 123400 = PriceFormatter.amount_to_cents("1234") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please check if you can copy some code from there:-
https://gist.github.com/perilbrain/62726f080e640e9b29ae