Loss of precision occurs when converting a BigDecimal value from a JSON

After making a network call, the backend is sending the following data:

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

However, when this information reaches the frontend and is logged using console.log:

{ 
  qty: 1.1234567890123457, 
  uom: "EA"
}

Why does it appear that JavaScript is either losing the last digit of the number or truncating it?

In Java, the number is represented using BigDecimal. The front end uses AngularJs framework and makes a GET request to the backend for a JSON response.

Answer №1

When it comes to Javascript floating point numbers, they have 64 bits of precision much like a double in Java. However, if you find yourself needing more precision beyond that, using BigDecimal in Java, you'll need to employ a similar approach in Javascript to surpass its limitations. One option is to utilize the NPM package js-big-decimal which allows you to transfer values as strings in JSON format to avoid them being interpreted as Numbers by JSON.parse.

Answer №2

Appreciate the assistance. I discovered that by including

@JsonProperty
public String retrieveQuantity(){
   return this.quantity;
}

in the Java model resolves the problem.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Retrieving the response value in AngularJS with $resource

I'm trying to retrieve the response of a request using $resource in AngularJS. Here is an example implementation: angular.module('app').factory('AuthResource', ['$resource', function($resource) { return { isA ...

The value returned by EntityRecognizer.resolveTime is considered as 'undefined'

In my bot's waterfall dialog, I am utilizing the LuisRecognizer.recognize() method to detect datetimeV2 entities and EntityRecognizer.resolveTime() to process the response. Here is an example of how I have implemented it: builder.LuisRecognizer.recog ...

Sending a string to an HTTPServlet using jQuery's Ajax method resulted in an empty request

I am currently working on sending data back to my HTTPServlet when a button is pressed. My ultimate goal is to send JSON data, but for now I am just focusing on sending back a simple string. Whenever I click the saveThis button, my server displays "Empty" ...

Inserting duplicate rows from CSV using JavaScript

Currently, I am utilizing Papaparse to sum up rows with duplicate SKUs in my CSV file. I'm striving to achieve this task without the use of additional JS libraries such as D3. Here is a snippet of how my CSV data is structured: SKU,Daily total,Weekly ...

Can someone clarify the distinction between returning a value directly or using Promise.resolve within the then() function?

What is the distinction between: new Promise(function(res, rej) { res("first example"); }) .then(function(result) { return "bbb"; // directly returning string }) .then(function(result) { console.log(result); }); and this: n ...

The Challenge of Logging in with the Loopback Angular SDK

I have set up a "user" named model with the base class of "User". I'm attempting to log in a user on my Angular App using the service generated by lb-ng, but it doesn't seem to be working. When I call User.login() in my Login controller, providin ...

Using Nuxt and Cloudinary to seamlessly upload images from the client side directly to the Cloudinary platform

Is there a way to directly upload images from my Nuxt (vue) app to Cloudinary without involving a server? I've been searching for information on how to accomplish this but haven't found any concrete solutions. <v-file-input v-else ...

Having trouble retrieving data using a custom URL in Axios with ReactJs

My knowledge of Reactjs is still new and I am currently working on a project using nextjs. I have a component called Trending.js that successfully fetches data from the URL "https://jsonplaceholder.typicode.com/users". However, when I try to change the U ...

"Utilizing the power of mapping in React to dynamically generate and return an

Hello everyone! I'm currently working on a project where I am making a get request to a Google API and fetching some data. Initially, the state value is empty, but after receiving the ajax response, I expect the state values to be updated using setSta ...

Vue.js event change method does not initiate the trigger

I have been attempting to implement a change event in my Vue application, where a statement switches between true and false each time I check a checkbox. However, the functionality doesn't seem to be working as expected. This issue arose while follow ...

JavaScript: function that operates asynchronously

I am new to JavaScript and encountered some issues with async functions. I have a function that fetches data from MongoDB by creating a promise, but it returns a collection with an empty object. async function getRating(item, applicant) { let arr = await ...

Ways to display the ChaptersButton in Videojs-Player

I'm trying to incorporate videojs (version 8.10.0) into my project and provide viewers with a way to select chapters within the video. According to the official documentation, it seems that simply including a track element linking to a VTT file within ...

React Error: Unable to iterate over this.state.descriptions

Currently facing an issue while trying to resolve this error https://i.stack.imgur.com/BZ304.png The goal is to generate an automated form using the following function: let descriptionsForm = ( <form> {this.state.descriptions.map((d ...

What methods can be employed to reduce additional background tasks when altering a state in a React component?

Trying out Code I experimented with creating a React exercise code that showcases a bus and its seats. Reserved seats are marked in red and cannot be selected, while the remaining seats can be chosen by clicking on them and selecting a gender from a popup ...

What is the method for sending a singular key (for example, 'k') to a window using Selenium WebDriver in Java?

As I create automated tests for a website that heavily relies on keyboard shortcuts for navigation, I have encountered an issue. While special key combinations like Ctrl + V, Alt + C work without a problem, sending single letters or num-pad symbols does no ...

Utilizing Angular's service for a dynamic typeahead feature

Whenever I try to utilize a service that returns a promise, the typeahead feature fails to work properly... You can view the error in this plunk. While using the first method (getLocation), everything runs smoothly... However, when attempting to use a ser ...

Utilizing Material UI (mui) 5.0 within an iframe: Tips and tricks

Attempting to display MUI components within an iframe using react portal has resulted in a loss of styling. Despite being rendered within the iframe, MUI components seem to lose their visual appeal when displayed this way. Most resources discussing this is ...

Columns that can be resized in a right-to-left (

Whenever I use RTL, the columns behave erratically when resizing... I have implemented colResizable. You can see an example here: http://jsfiddle.net/r0rfrhb7/ $("#nonFixedSample").colResizable({ fixed: false, liveDrag: true, gripInnerHtml: "<d ...

Issues arise when trying to use the Jquery append() method in conjunction with Angular

We are currently utilizing jquery version 1.9.1 and angular version 1.2.13 for our project. Our wysiwyg editor is functioning well, as we are able to save HTML into the database and load it back using the jquery append function successfully. However, we ar ...

Enabling Unverified SSL Certificates in Opera Browser for Selenium Testing using Java

Is there a way to configure Selenium WebDriver to accept untrusted certificates on the Opera browser? I attempted to implement this code, but it did not produce the desired outcome. DesiredCapabilities capabilities = new DesiredCapabilities(); capabiliti ...