klo

klo

Anyway to get a Float from 1.0 to 1.00?

Is there any way to go from a float 1.0 to 1.00 while retaining the float() type?

Most Liked

LostKobrakai

LostKobrakai

Is that a json api? If so you’d need to look into your json libraries encoding function. But in the end if you’re not formatting as a string there’s likely no difference. Unless the other end parses the float number back into a decimal, but again into a float value it’ll always become 1.0. Floats do not explicitly store decimal points and when the binary float format is converted to a string for inspection it’s just printed with the least amout of decimal points.

garrison

garrison

Floats are not actually stored as 1.0 and so on, they have a different internal representation.

Can you explain what it is you’re trying to accomplish?

garrison

garrison

Okay, so the actual question is “how do I get the JSON encoder to format floats with extra precision”. We don’t know what JSON library you’re using, but the Erlang :json just calls float_to_binary. It looks like the encoder function can be overridden, so you could provide one that encodes floats with extra zeros if you like.

However, as suggested above this is probably a waste of time. The consumer of the API should just decode the value back into a float, in which case it won’t matter. But if you have some weird requirement for this behavior, that’s how you would do it.

knoebber

knoebber

It’s also worth remembering that JSON doesn’t have an integer or float type, only numbers. Different JSON libraries handle numbers in different ways. For example, golang treats all JSON numbers as floats by default (even when the number doesn’t have a decimal point). So, it’s undefined what will happen to precision when you send a number over JSON.

eksperimental

eksperimental

You will need something that will format your floats. The Number library does this.

Where Next?

Popular in Troubleshooting Top

chocolatedonut
Besides (over)logging every code-path leading to Repo.insert_all, is it possible to make Postgrex and/or ecto_sql log the exact, failing ...
New
mooreryan
In a project I’m working on, I cannot get tests with breakpoints or pry working. Here is an example in a new mix project. Create a fresh...
New
wktdev
I tried to learn Elixir a few years ago and stopped. I am trying again. In the code below I have a GenServer that is started by a Superv...
New
jdj_dk
Hi everyone. I’m trying to setup a small cluster with three nodes. I’m using libcluster as I have before. But my nodes doesn’t have a us...
New
pikdum
Hello, I’m trying to upgrade a pretty large project from Elixir 1.18.4 to Elixir 1.19.1 (1321 modules), but seeing some compiler warning...
New
apoorv-2204
The Issue of Enums in Elixir Elixir doesn’t have a native enum construct, so we usually rely on atoms, strings, or macros to represent na...
New
klo
Is there any way to go from a float 1.0 to 1.00 while retaining the float() type?
New
rayex
Hi, I am using the following versions: Erlang/OTP 27 [erts-15.1] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit:ns] El...
New
Richardm
Hi, I have the following code, and even though I can see (via the inspect) that the response_body does contain one of the strings I’m lo...
New
runyonave
We have been stuck on this for a few days now and I am really not sure what could be missing. I can get chromic_pdf to work perfectly fin...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
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
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
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

We're in Beta

About us Mission Statement