skip to Main Content

The network call shows that the backend is sending this:

"uom" : "EA",
"qty" : 1.123456789012345678

but when it reaches the frontend, using console.log:

{ 
  qty: 1.1234567890123457, 
  uom: "EA"
}

why is the javascript losing the last digit or truncating the number?

The number is represented using a BigDecimal in java. The frontend use AngularJs framework, makes a GET call to the backend requesting a JSON.

2

Answers


  1. Chosen as BEST ANSWER

    Thanks for the help. I found that adding

    @JsonSerialize(using = ToStringSerializer.class)
    public getQty(){
       return this.qty;
    }
    

    to the java domain object solves the issue.


  2. Javascript floating point numbers hold 64 bits of precision, similar to a double in Java. If you have to use BigDecimal in Java to extend beyond that level of precision, you’ll need to do something similar in Javascript to extend beyond its similar boundary. The NPM package js-big-decimal is probably your best bet and you can transfer the values as strings in JSON to prevent JSON.parse from interpreting them as Number.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search