skip to Main Content

I have a response from the web service and this response is a json type string. I get the STHAR_GCMIK value here using obj.getString().

This code works correctly up to 6 digit values, but it returns 4.0E+7 for the value 4000000. I tried different solutions but failed. How can I display the value here as it is?


private AttrsSiparis parseSiparisBilgisi(String data) {
        AttrsSiparis siparisBilgileri = new AttrsSiparis();
        try {
            JSONArray jsonArray = new JSONArray(data);
            for(int i = 0; i < jsonArray.length(); i++)
            {
                JSONObject obj = jsonArray.getJSONObject(i);
                Log.i(Constants.TAG, "Irsaliye Result Parse : STHAR_GCMIK " + obj.getString("STHAR_GCMIK"));

                //siparisBilgileri.miktarlar.add(obj.getString("STHAR_GCMIK"));
                siparisBilgileri.miktarlar.add(new BigDecimal(obj.getString("STHAR_GCMIK")).toString());
                
            }
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
        return siparisBilgileri;
    }

3

Answers


  1. Chosen as BEST ANSWER

    I solved this error by first converting the incoming data to double type and then formatting it as string type.

    siparisBilgileri.miktarlar.add(String.format("%.0f",Double.parseDouble(obj.getString("STHAR_GCMIK"))));
    

  2. How can I display the value here as it is?

    I guess you are talking about displaying the already parsed BigDecimal. If so, then if you are logging or printing it, it uses its toString() method. This method uses the scientific notation by default.

    If you want to display the number as it is You have to use the toPlainString() method – it doesn’t use the scientific notation.

    Login or Signup to reply.
  3. You can format it to double via .getDouble("STHAR_GCMTK") and then convert that to String.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search