1

What to do If I have a big double and I want to output it into json document as a number like so 222222222.25 ?

I found many solutions but they all handle this as a string:

 System.out.println("DOUBLE1: " + String.format("%.2f", 222222222.2502));
 System.out.println("DOUBLE2: " + String.valueOf(new Double(String.format("%.2f", 222222222.2502))));

Output : DOUBLE1: 222222222.25 DOUBLE2: 2.2222222225E8

So what I want is to have the number 222222222.25 in json. How to do that?

I need to get the following using jackson: {value: 222222222.25} and not: {value: "222222222.25"}

taz_
  • 137
  • 3
  • 13
  • Can you share your Jackson code with us? AFAIK if your POJO uses a `Double` or `BigDecimal` then Jackson should use the correct serialization as a number automatically. – Tim Biegeleisen Dec 19 '17 at 01:50
  • The problem is the json value property can be either an integer, a double, or a string. So I am mapping to an Object and deciding what the value is. The document model is also very big to post here. The mapping is no problem but I want to have the non scientific notation in the json. – taz_ Dec 19 '17 at 01:55
  • Just the same type consistently throughout. If you need to work the field as a number, then use a numeric type. – Tim Biegeleisen Dec 19 '17 at 01:56

1 Answers1

1

You should convert string to double before writing it to JSON file.

Use Double.parseDouble to get it work.

Eugene
  • 10,627
  • 5
  • 49
  • 67
  • Hmm, doesn seem to work, or I am doing something wrong ... – taz_ Dec 19 '17 at 02:36
  • Let me clarify whether I get your point clearly, do you want put the double value into the JSON file without the "E8" suffix? – Eugene Dec 19 '17 at 02:39
  • Yes, as a number, and not a string, {"name": "someName", "value": 2222222.22, "something": "something", ... } – taz_ Dec 20 '17 at 06:59