user1935987 user1935987 - 6 months ago 57
JSON Question

Jackson Mapper integer from json parsed as double with drong precision

I had a working solution, but after making a multiple changes on project it doesn't work anymore.

The problem:
Front-end sending integer value

which is an attribute of the object converted by Jackson
. i need to get a double with precision 2 from this integer value.

This is the java model:

public class Item {

private int condoId;
private Integer itemId;
private String owner;

@Size(min=1, max=60)
private String itemTitle;

@Size(min=1, max=1000)
private String itemDescr;

@Size(min=1, max=45)
private String itemEmail;

@Size(min=1, max=16)
private Double itemPrice;

Was working before:

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

*In controller, received a request data as string data*

ObjectMapper mapper = new ObjectMapper();
JsonNode node = null;
node = mapper.readTree(data);
Item item = mapper.convertValue(node.get("item"), Item.class);
NumberFormat formatter = new DecimalFormat(".00");

Now this solution giving me wrongly formatted values (like
) from
What is the way to convert the received integer attribute to the required double?

PS. in
DB it is stored as

probably the right question is - why the received
int 567657654
from json POST data is deserialized to
, which is a "solid" 5 + decimal


There is no precision problem here. Every digit of the input appears in the output. You are merely seeing, but not recognizing, scientific notation for floating-point. The reason for that lies on your own code: you have an itemPrice column declared as NUMERIC(10,2) but you have mapped that to a Java Double. This is not correct: it is causing this problem; and it will cause untold others further down the track.

The correct Java type to use for that is not Double, or double, but BigDecimal.

Never use floating-point for money.