SoftBank.Cldr.Money.from_float

You're seeing just the function from_float, go back to SoftBank.Cldr.Money module for more information.
Link to this function

from_float(currency_code, amount, options \\ [])

View Source (since 2.0.0)

Specs

from_float(
  float() | Money.currency_code(),
  float() | Money.currency_code(),
  Keyword.t()
) :: Money.t() | {:error, {module(), String.t()}}

Returns a %:'Elixir.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_code is an ISO4217 three-character upcased binary or atom

  • amount is a float

Examples

iex> SoftBank.Cldr.Money.from_float 1.23456, :USD
#Money<:USD, 1.23456>

iex> SoftBank.Cldr.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"}}