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

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews