henriquesati

henriquesati

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)

Showing Posts 1 to 4

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? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
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
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews