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

alexlanderzander
Hello everyone, I’m working on a blockchain project in Elixir and I’m implementing some of the core cryptography in a Rust NIF using Rus...
New
harmon25
Hi All, I am bumping into a weird issue with an umbrella app while upgrading to latest Elixir 1.19.x + otp28. A couple apps in the umbr...
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
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
michaelterryio
Hey, I’ve got a project with several path dependencies. Some are phoenix apps, but this likely isn’t relevant. I don’t fully understand...
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
onelastdance
Hey there, Using the install script method recommended here on Ubuntu 24.04: And then running the following commands: iex> :obser...
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
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

Other popular topics Top

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
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
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
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
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
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New

We're in Beta

About us Mission Statement