0

Hot to get exact value of key "totalValue" from the json mentioned below: (Note: I tired json.getdouble("totalValue") but not getting the exact value of key "totalValue"

Code Snippet:

String jsonStrTemp="    {\n" +
        "        \"branchName\": \"CAG MUMBAI\",\n" +
        "        \"branchId\": \"51\",\n" +
        "        \"totalValue\": 177777783312648600000\n" +
        "      }";
try {
    JSONObject jsonObj=new JSONObject(jsonStrTemp);
    System.out.println("testDB jsonStr getLong : "+jsonObj.getLong("totalValue"));
    System.out.println("testDB jsonStr getDouble : "+jsonObj.getDouble("totalValue"));

} catch (JSONException e) {
    System.out.println("testDB JSONException");
}

Output:

System.out: testDB jsonStr getLong : 9223372036854775807 
System.out: testDB jsonStr getDouble : 1.777777833126486E20 
System.out: testDB jsonStr getString : 1.777777833126486E20
KDeogharkar
  • 10,939
  • 7
  • 51
  • 95
pravingaikwad07
  • 482
  • 1
  • 7
  • 24

1 Answers1

0

This: 1.777777833126486E20 represents the exponential notation of a number. That means the number is equals to the number preceding the letter E times 10^pow, where pow is the part of the number after the letter E. In this case pow=20, number = 1.1.777777833126486 * 10^20.

If you wish to format this any other way, refer to this question.

Community
  • 1
  • 1
Vucko
  • 7,371
  • 2
  • 27
  • 45
  • Thanks.Resolved the issue by doing following:- String formatStr = String.format("%f", jsonValue); double lacCr = Double.parseDouble(formatStr); //where jsonValue= jsonObj.getDouble("totalValue") – pravingaikwad07 Jun 13 '16 at 09:03
  • Cool. I didn't want to write how to format since the question I linked clearly explained it. Consider accepting the answer to close the question. Thanks. – Vucko Jun 13 '16 at 09:12