Add to format_according_to_decimals/2 supports integers

pull/534/head
Felipe Renan 6 years ago
parent 3970233b3a
commit 3eb7c9f7ec
  1. 10
      apps/block_scout_web/lib/block_scout_web/views/currency_helpers.ex
  2. 7
      apps/block_scout_web/test/block_scout_web/views/currency_helpers_test.exs

@ -65,7 +65,17 @@ defmodule BlockScoutWeb.CurrencyHelpers do
iex> format_according_to_decimals(Decimal.new(205000), 2) iex> format_according_to_decimals(Decimal.new(205000), 2)
"2,050" "2,050"
iex> format_according_to_decimals(205000, 2)
"2,050"
""" """
@spec format_according_to_decimals(non_neg_integer(), non_neg_integer()) :: String.t()
def format_according_to_decimals(value, decimals) when is_integer(value) do
value
|> Decimal.new()
|> format_according_to_decimals(decimals)
end
@spec format_according_to_decimals(Decimal.t(), non_neg_integer()) :: String.t() @spec format_according_to_decimals(Decimal.t(), non_neg_integer()) :: String.t()
def format_according_to_decimals(%Decimal{sign: sign, coef: coef, exp: exp}, decimals) do def format_according_to_decimals(%Decimal{sign: sign, coef: coef, exp: exp}, decimals) do
sign sign

@ -49,6 +49,13 @@ defmodule BlockScoutWeb.CurrencyHelpersTest do
assert CurrencyHelpers.format_according_to_decimals(amount, decimals) == "10,004.5" assert CurrencyHelpers.format_according_to_decimals(amount, decimals) == "10,004.5"
end end
test "supports value as integer" do
amount = 1_000_450
decimals = 2
assert CurrencyHelpers.format_according_to_decimals(amount, decimals) == "10,004.5"
end
end end
describe "format_integer_to_currency/1" do describe "format_integer_to_currency/1" do

Loading…
Cancel
Save