Skip to content

Instantly share code, notes, and snippets.

@unnawut
Last active May 11, 2019 10:26
Show Gist options
  • Save unnawut/c284a5226cb56096ea6092f75d7f5cab to your computer and use it in GitHub Desktop.
Save unnawut/c284a5226cb56096ea6092f75d7f5cab to your computer and use it in GitHub Desktop.
Tinkering with decimals

GO.Exchange

(max 8 decimal points) API returns amounts and rates as string Lowest: OMG/BTC 0.00023935 Highest: BTC/USDC 6674.34

Binance

(max 8 decimal points) API returns amounts and rates as string Lowest: NPXS/BTC 0.00000001 Highest: BCHABC/BTC 0.044838

BX

(max 8 decimal points) API returns amounts and rates as string Lowest: HYP/BTC 0.00000005 Highest: BTC/THB 219849.95

Kraken

(max 6 decimal points) API returns amounts and rates as string Lowest: DOGE/BTC 0.00000038 Highest: BTC/JPY 687798.0000

# Take decimal.exs from https://github.com/ericmj/decimal/blob/master/lib/decimal.ex
Code.require_file("decimal.exs")
IO.puts("----- ASSUMPTIONS -----")
precision = 36
rounding = :half_even
Decimal.get_context()
|> Map.put(:precision, precision)
|> Map.put(:rounding, rounding)
|> Decimal.set_context()
IO.puts("Precision is set to #{inspect(precision)}")
IO.puts("Rounding is set to #{inspect(rounding)}")
# Smallest number (18 decimal points, anything smaller is rounded off and represented as exponents
{:ok, smallest} = Decimal.parse("0.111111111111111111")
IO.puts("Assuming the smallest amount possible is #{Decimal.to_string(smallest)}")
# Largest number (18 digits), anything larger is rounded off and represented as exponents
{:ok, largest} = Decimal.parse("111111111111111111.0")
IO.puts("Assuming the largest amount possible is #{Decimal.to_string(largest)}")
IO.puts("\n----- CALCULATIONS -----")
# Smallest -> smallest
# smallest * rate = smallest
# rate = smallest / smallest
smallest
|> Decimal.div(smallest)
|> IO.inspect(label: "Rate for smallest -> smallest")
# Largest -> largest
# largest * rate = largest
# rate = largest / largest
smallest
|> Decimal.div(smallest)
|> IO.inspect(label: "Rate for largest -> largest")
# Smallest -> largest
# smallest * rate = largest
# rate = largest / smallest
largest
|> Decimal.div(smallest)
|> IO.inspect(label: "Rate for smallest -> largest")
# Largest -> Smallest
# largest * rate = smallest
# rate = smallest / largest
smallest
|> Decimal.div(largest)
|> IO.inspect(label: "Rate for largest -> smallest")
$ elixir decimals.exs
----- ASSUMPTIONS -----
Precision is set to 36
Rounding is set to :half_even
Assuming the smallest amount possible is 0.111111111111111111
Assuming the largest amount possible is 111111111111111111.0
----- CALCULATIONS -----
Rate for smallest -> smallest: #Decimal<1>
Rate for largest -> largest: #Decimal<1>
Rate for smallest -> largest: #Decimal<1.0E+18>
Rate for largest -> smallest: #Decimal<1E-18>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment