Current section
Files
Jump to
Current section
Files
src/dysmal.erl
-module(dysmal).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/dysmal.gleam").
-export([from_string/1, from_string_with_opts/2, to_string/1]).
-export_type([decimal/0, opts/0, rounding_algorithm/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-type decimal() :: any().
-type opts() :: {opts, integer(), rounding_algorithm()}.
-type rounding_algorithm() :: round_half_up |
round_half_down |
round_half_even |
round_up |
round_down |
round_ceiling |
round_floor.
-file("src/dysmal.gleam", 21).
?DOC(" Default options for precision and rounding\n").
-spec default_opts() -> opts().
default_opts() ->
{opts, 2, round_half_up}.
-file("src/dysmal.gleam", 51).
?DOC(" Helper to convert rounding algorithm to atom\n").
-spec rounding_to_atom(rounding_algorithm()) -> any().
rounding_to_atom(Rounding) ->
case Rounding of
round_half_up ->
erlang:binary_to_atom(<<"round_half_up"/utf8>>);
round_half_down ->
erlang:binary_to_atom(<<"round_half_down"/utf8>>);
round_half_even ->
erlang:binary_to_atom(<<"round_half_even"/utf8>>);
round_up ->
erlang:binary_to_atom(<<"round_up"/utf8>>);
round_down ->
erlang:binary_to_atom(<<"round_down"/utf8>>);
round_ceiling ->
erlang:binary_to_atom(<<"round_ceiling"/utf8>>);
round_floor ->
erlang:binary_to_atom(<<"round_floor"/utf8>>)
end.
-file("src/dysmal.gleam", 26).
?DOC(" Convert Opts type to a Dict\n").
-spec opts_to_dict(opts()) -> gleam@dict:dict(any(), integer()).
opts_to_dict(Opts) ->
{opts, Precision, Rounding} = Opts,
Rounding_atom = rounding_to_atom(Rounding),
_pipe = maps:new(),
_pipe@1 = gleam@dict:insert(
_pipe,
erlang:binary_to_atom(<<"precision"/utf8>>),
Precision
),
gleam@dict:insert(
_pipe@1,
erlang:binary_to_atom(<<"rounding"/utf8>>),
Rounding_atom
).
-file("src/dysmal.gleam", 64).
?DOC(" Create a new Decimal from a string.\n").
-spec from_string(binary()) -> decimal().
from_string(Value) ->
decimal:to_decimal(Value, opts_to_dict(default_opts())).
-file("src/dysmal.gleam", 69).
?DOC(" Create a new Decimal from a String with precision and rounding options.\n").
-spec from_string_with_opts(binary(), opts()) -> decimal().
from_string_with_opts(Value, Opts) ->
decimal:to_decimal(Value, opts_to_dict(Opts)).
-file("src/dysmal.gleam", 74).
?DOC(" Convert a Decimal to a String.\n").
-spec to_string(decimal()) -> binary().
to_string(Decimal) ->
decimal:to_binary(Decimal).