eksperimental

eksperimental

How to get the ordinary decimal notation of a float

iex> 75000 / 12.5
6.0e3

iex(14)> 75000 / 12.5 |> Float.to_string                      
"6.0e3"

It is kind of weird that Elixir is formatting such a small number using the scientific notation. Is there a way around to display and/or convert to string the float and get 6000.0 and not 6.0e3?

Cheers

Marked As Solved

outlog

outlog

iex> 75000/12.5 |> :erlang.float_to_binary([ decimals: 2]) 
"6000.00"

or

iex> 75000/12.5 |> :erlang.float_to_binary([:compact, decimals: 2])
"6000.0"

Also Liked

alco

alco

The documentation for Float.to_string/1 says

It uses the shortest representation

so I guess that’s why it chooses "6.0e3" over 6000.0, the latter is one character longer.

Take a look at :erlang.float_to_binary/2, here’s how it can be used:

iex(18)> max_precision = 6
6
iex(19)> :erlang.float_to_binary(6000.0, [:compact, decimals: max_precision])
"6000.0"
eksperimental

eksperimental

Thank you @outlog and @alco for your answers.
Unfortunately it is not easy to do this within Elixir itself.
Now I see that Float.to_string/2 had been deprecated and it accepts the same options as :erlang.float_to_binary/2

Where Next?

Popular in Questions Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
gazoon
I want to know absolute current module path. In python i could do that: os.path.abspath(__file__) Does elixir have anything similar?
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

Other popular topics Top

freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement