Fix format_according_to_decimals method

pull/4888/head
Viktor Baranov 3 years ago
parent 3b460b3171
commit ed034c56b5
  1. 19
      apps/block_scout_web/lib/block_scout_web/views/currency_helpers.ex

@ -42,6 +42,15 @@ defmodule BlockScoutWeb.CurrencyHelpers do
iex> format_according_to_decimals(205000, Decimal.new(2)) iex> format_according_to_decimals(205000, Decimal.new(2))
"2,050" "2,050"
iex> format_according_to_decimals(105000, Decimal.new(0))
"105,000"
iex> format_according_to_decimals(105000000000000000000, Decimal.new(100500))
"105"
iex> format_according_to_decimals(105000000000000000000, nil)
"105,000,000,000,000,000,000"
""" """
@spec format_according_to_decimals(non_neg_integer() | nil, nil) :: String.t() @spec format_according_to_decimals(non_neg_integer() | nil, nil) :: String.t()
def format_according_to_decimals(nil, _) do def format_according_to_decimals(nil, _) do
@ -60,9 +69,13 @@ defmodule BlockScoutWeb.CurrencyHelpers do
@spec format_according_to_decimals(Decimal.t(), Decimal.t()) :: String.t() @spec format_according_to_decimals(Decimal.t(), Decimal.t()) :: String.t()
def format_according_to_decimals(value, decimals) do def format_according_to_decimals(value, decimals) do
value if Decimal.cmp(decimals, 18) == :gt do
|> divide_decimals(decimals) format_according_to_decimals(value, Decimal.new(18))
|> thousands_separator() else
value
|> divide_decimals(decimals)
|> thousands_separator()
end
end end
defp thousands_separator(value) do defp thousands_separator(value) do

Loading…
Cancel
Save