henriquesati

henriquesati

Decimal not returning a number

I’m passing a string to decimal.new() and instead of a float it’s returning some data type that not allows me to do aritmetic expressions

 def get_USD_amount_basedOn_BRL(brl_amount) do
    {_status_code, brl_usd_price_str} = get_brl_usd()
    brl_usd_price = Decimal.new(brl_usd_price_str)
    IO.inspect(brl_usd_price)
  end
end

output expected:

5.65

output received:

Decimal.new("5.5936")

error received when I try to execut arithmetic expressions

** (ArithmeticError) bad argument in arithmetic expression
    (testes_pay 0.1.0) lib/UsdBrl.ex:24: UsdBrl.get_USD_amount_basedOn_BRL/1
    lib/main.ex:25: (file)

First 4 of 4 Posts Switch mode

cmkarlsson

cmkarlsson

Decimal.new/1 returns a Decimal struct. You need to use the Decimal module to do arithmetic operations.

iex(3)> d1 = Decimal.new("5.5623")
Decimal.new("5.5623")
iex(4)> d2 = Decimal.new("3.12")
Decimal.new("3.12")
iex(5)> Decimal.mult(d1, d2)
Decimal.new("17.354376")
iex(6)> Decimal.mult(d1, d2) |> Decimal.round(3)
Decimal.new("17.354")
iex(7)> Decimal.mult(d1, d2) |> Decimal.round(3) |> Decimal.to_float
17.354

Using the decimal for calculations is strongly recommended as using floats is not precise(Floating-point arithmetic - Wikipedia) and can lead to thing like this:

iex(9)> 0.1 + 0.2
0.30000000000000004
# Instead use Decimal
iex(10)> Decimal.add(Decimal.from_float(0.1), Decimal.from_float(0.2)) |> Decimal.to_float
0.3

Check Decimal — Decimal v3.1.1 for more information.

In addition if you are working with money you might want to check out: ex_money | Hex. It is very comprehensive but may be overkill for your situation.

henriquesati

henriquesati OP

Thanks! and it is possible to turn the final data after all the operations into a normal decimal number?

cmkarlsson

cmkarlsson

Yes. The Decimal module has got functions to convert the result into integers, floats and strings. Often you don’t have to convert it back to a float or other numbers but just to strings for the presentation layer.

EDIT: Some exampels

iex(11)> Decimal.to_string(d1)
"5.5623"
iex(12)> Decimal.to_float(d1)
5.5623
iex(13)> Decimal.round(d1, 0) |> Decimal.to_integer
6
jswanner

jswanner

DecimalEnv can make it less verbose to deal with decimal arithmetic

— All posts loaded —

Where Next?

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement