0

The code is as below:

float f = 1.7f;
JSONArray ja = new JSONArray();
try {
    ja.put(f);
    Log.d("json", "f=" + f);
    Log.d("json", "ja=" + ja.toString());
} catch (JSONException e) {
    e.printStackTrace();
}

The output is:

05-16 17:49:30.559  32554-32554/com.group.home D/json﹕ f=1.7
05-16 17:49:30.559  32554-32554/com.group.home D/json﹕ ja=[1.7000000476837158]

If f is 1.0, then the output will be ja=1.

Where does the 000000476837158 come from? How should I prevent this from happening?

Bruno Parmentier
  • 1,219
  • 2
  • 14
  • 34
mianlaoshu
  • 2,342
  • 3
  • 27
  • 48

1 Answers1

0

There is no JSONArray#put(float) in the JSONArray class so your f variable is converted to a double.

Take a look at this question: Convert float to double without losing precision.

Community
  • 1
  • 1
Bruno Parmentier
  • 1,219
  • 2
  • 14
  • 34