2

I'm trying to add an double value to an JSONObject, its automatically recognising as int(code below). After jsonObject.put the unitCost is converted to integer automatically to {"unitCost":100} rather than {"unitCost":100.0}. Please help i need to keep it as double.Thanks

Double unitCost = 100.0D;

JSONObject jsonObject = new JSONObject();

jsonObject.put("unitCost", unitCost);
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198

2 Answers2

1

It will be better if you use String

Strings are constant;

String unitCost = "100.0";
JSONObject jsonObject = new JSONObject();
jsonObject.put("unitCost", unitCost);
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
0

Its not converting double into int. If the first decimal digit is 0, it won't be visible, Change it to 100.1 , you will be able to see the double value.

Dishonered
  • 8,449
  • 9
  • 37
  • 50