Packages

Converts BTC to Satoshi, with some helper utils meant to work with tracking/converting cryptocurrencies.

Current section

Files

Jump to
satoshi_ex lib satoshi utils number.ex
Raw

lib/satoshi/utils/number.ex

defmodule Satoshi.Utils.Number do
@doc """
Converts Int to x Significant Figures.
Returns an Integer.
## Examples
iex> Number.to_sf(0.05555, 3)
0.0556
"""
def to_sf(amount, _) when amount == 0, do: 0
def to_sf(amount, sf) do
round_amount = sf - round(Float.ceil(Math.log10(amount)))
if round_amount < 0 do
x = String.to_integer("1" <> String.duplicate("0", -round_amount))
round(amount/x) * x
else
Float.round(amount, round_amount)
end
end
end