Money.from_float
from_float, go back to Money module for more information.
Specs
from_float(float() | currency_code(), float() | currency_code(), Keyword.t()) :: t() | {:error, {module(), String.t()}}
Returns a %Money{} struct from a currency code and a float amount, or
an error tuple of the form {:error, {exception, message}}.
Floats are fraught with danger in computer arithmetic due to the unexpected loss of precision during rounding. The IEEE754 standard indicates that a number with a precision of 16 digits should round-trip convert without loss of fidelity. This function supports numbers with a precision up to 15 digits and will error if the provided amount is outside that range.
Note that Money cannot detect lack of precision or rounding errors
introduced upstream. This function therefore should be used with
great care and its use should be considered potentially harmful.
Arguments
currency_codeis an ISO4217 three-character upcased binary or atomamountis a floatoptionsis a keyword list of options passed toMoney.new/3. The default is[].
Examples
iex> Money.from_float 1.23456, :USD
#Money<:USD, 1.23456>
iex> Money.from_float 1.234567890987656, :USD
{:error,
{Money.InvalidAmountError,
"The precision of the float 1.234567890987656 is " <>
"greater than 15 which could lead to unexpected results. " <>
"Reduce the precision or call Money.new/2 with a Decimal or String amount"}}