0

I have an array from JSON like below :

"lodg": [82015372.06, 0, 0, 82015372.06, 82486446.4401653]

I can't get the real value 82015372.06 . I try some code like below :

Double integer = obj.getJSONArray("lodg").getDouble(3); // 8.201537206E7
Long value = Double.valueOf(integer).longValue(); //82015372
float fl = (float) obj.getJSONArray("lodg").getDouble(3); //8.2015376E7

I read questions regarding this but it does not work. I need to get the value, not only just print out.

halfer
  • 19,824
  • 17
  • 99
  • 186
MrX
  • 953
  • 2
  • 16
  • 42
  • 1
    try formatting your double with [DecimalFormat](https://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html) – Scary Wombat Apr 13 '18 at 06:30
  • see https://stackoverflow.com/questions/322749/retain-precision-with-double-in-java – Scary Wombat Apr 13 '18 at 06:31
  • @ScaryWombat I have a question. If I use that value (`8.201537206E7`), is it same with real value (`82015372.06`) ? – MrX Apr 13 '18 at 06:42
  • If you consider int values then between MIN_VALUE and MAX_VALUE there are an exact amount and they can all fit within the allocated space (e.g. 64Bits) but of course there are an infinate number of floating points. Not an easy read but have a look at https://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.2.3 – Scary Wombat Apr 13 '18 at 06:51
  • @ScaryWombat ok thank you very much for all the link, I will try add some code now – MrX Apr 13 '18 at 07:21
  • 1
    this is the correct way(your first statement): `Double integer = obj.getJSONArray("lodg").getDouble(3); // 8.201537206E7` – ACV Sep 24 '18 at 09:55
  • 2
    what is wrong with `8.201537206E7` - that is exactly `82015372.06` (despite doubles are not always indicated if you need **exact** values - check [BigDecimal](https://docs.oracle.com/javase/10/docs/api/java/math/BigDecimal.html)) Also have a look at [Scientific Notation](https://en.wikipedia.org/wiki/Scientific_notation) – user85421 Sep 24 '18 at 09:58

0 Answers0