0

Greeting. 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 565656 which is an attribute of the object converted by Jackson ObjectMapper. 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;

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

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

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

    @NotNull
    @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");
            item.setItemPrice(Double.parseDouble(formatter.format(item.getItemPrice())));

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

PS. in PostgreSQL DB it is stored as numeric(10,2).

EDIT: probably the right question is - why the received int 567657654 from json POST data is deserialized to double 5.67657654E8, which is a "solid" 5 + decimal .67657654E8

user1935987
  • 3,136
  • 9
  • 56
  • 108
  • 2
    There is ***no such thing*** as a 'double with precision 2'. The precision of `double` is *fixed* at 53 binary digits. – user207421 Nov 30 '16 at 10:00
  • you can do something like this http://stackoverflow.com/questions/11520781/serialize-a-double-to-2-decimal-places-using-jackson in setter for customization while deserialize. – TechnoCrat Nov 30 '16 at 10:04
  • @EJP I guess he just wants to format with a couple of zeroes since int hasn't a decimal part anyways. – m0skit0 Nov 30 '16 at 10:04
  • @m0skit0. right.But somehow it's formatted from the int `567657654` to double `5.67657654E8` which is a "solid" 5 and 0.67657654E8 – user1935987 Dec 01 '16 at 04:51
  • 1
    So fix your question, and especially the title, but this is just scientific notation. There isn't even any change in precision. The only issue is why an `int` came back as a `double`, and the answer to that lies directly in your own code, where you have declared `Double itemPrice`. Don't use floating-point for money. This should be a `BigDecimal`. – user207421 Dec 01 '16 at 05:14
  • Integer doesn't have any decimal part, why you want to convert to double. – dvsakgec Dec 01 '16 at 05:32

1 Answers1

3

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.

user207421
  • 305,947
  • 44
  • 307
  • 483