acrolink

acrolink

Decimal received as {coef: 23652, exp: 0, sign: 1} at JS frontend (VueJS)

I am having a strange issue. Using Poison I encode object properties as JSON and send them to the VueJS front-end. One of the properties is a decimal field. The value is read by VueJS as something like: {coef: 23652, exp: 0, sign: 1}. How to send the decimal from the server to the front-end as a normal decimal (string like e.g. 342.23)

Marked As Solved

acrolink

acrolink

@peerreynders

Thank you. I have tried to use the above mentioned JS libraries and others but provided the Elixir’s values for coef, exp and sign, the JS libraries produce different results.

I have tried to encode Elixir-side the decimal as string by:

defimpl Poison.Encoder, for: Acrosap.Orders.Order do
  def encode(%{doctotal: doctotal}, options) do
    Poison.Encoder.BitString.encode("#{to_string(doctotal)}", options)
  end
end

But nothing happens (still sent to front-end as decimal object). What might be wrong with my code? Thank you.

UPDATE

I was able to convert the decimal to string before sending the response to the front-end by:

defimpl Poison.Encoder, for: Decimal do
  def encode(decimal, options) do
     Decimal.to_string(decimal, :normal)
    |> Poison.Encoder.encode(options)
  end
end

I tried to do the same with the Jason library but for some reason, it doesn’t work.

Also Liked

peerreynders

peerreynders

That is the standard encoding of the internal representation for Decimal.

For a different encoding implement a struct specific encoder.


https://github.com/MikeMcl/decimal.js/
https://github.com/MikeMcl/bignumber.js/
https://blog.enuma.io/update/2019/01/31/safe-use-of-bignumber.js.html


(string like e.g. 342.23 )

If that is represented as %Decimal{coef: 34223, exp: -2, sign: 1} and you are using BigNumber.js then "34223e-2" may be the preferable approach.

Where Next?

Popular in Questions Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
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
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics Top

hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
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 31494 112
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement