Fix format_according_to_decimals method

pull/4888/head
Viktor Baranov 3 years ago
parent 3b460b3171
commit ed034c56b5
  1. 13
      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,10 +69,14 @@ 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
if Decimal.cmp(decimals, 18) == :gt do
format_according_to_decimals(value, Decimal.new(18))
else
value value
|> divide_decimals(decimals) |> divide_decimals(decimals)
|> thousands_separator() |> thousands_separator()
end end
end
defp thousands_separator(value) do defp thousands_separator(value) do
if Decimal.to_float(value) > 999 do if Decimal.to_float(value) > 999 do

Loading…
Cancel
Save