diff --git a/apps/block_scout_web/lib/block_scout_web/views/currency_helpers.ex b/apps/block_scout_web/lib/block_scout_web/views/currency_helpers.ex index 0e3f357225..e6952eb662 100644 --- a/apps/block_scout_web/lib/block_scout_web/views/currency_helpers.ex +++ b/apps/block_scout_web/lib/block_scout_web/views/currency_helpers.ex @@ -65,7 +65,17 @@ defmodule BlockScoutWeb.CurrencyHelpers do iex> format_according_to_decimals(Decimal.new(205000), 2) "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() def format_according_to_decimals(%Decimal{sign: sign, coef: coef, exp: exp}, decimals) do sign diff --git a/apps/block_scout_web/test/block_scout_web/views/currency_helpers_test.exs b/apps/block_scout_web/test/block_scout_web/views/currency_helpers_test.exs index 4b00a6ad0d..b0931b9298 100644 --- a/apps/block_scout_web/test/block_scout_web/views/currency_helpers_test.exs +++ b/apps/block_scout_web/test/block_scout_web/views/currency_helpers_test.exs @@ -49,6 +49,13 @@ defmodule BlockScoutWeb.CurrencyHelpersTest do assert CurrencyHelpers.format_according_to_decimals(amount, decimals) == "10,004.5" 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 describe "format_integer_to_currency/1" do