String JSON - Two decimal places

Asked

Viewed 768 times

1

I’m reading a JSON object, and I can get the data from TextView. However, in the value of String Preco when the price has a zero in the second decimal place, it does not print in the TextView. For example: 9.20 display 9.2. I tried some functions, however, without success. Can someone help me ?

    @Override
    protected void onPostExecute(String[] jsonWinthor) {
        txtCodProd.setText(jsonWinthor[0]);
        txtDescProduto.setText(jsonWinthor[1]);
        txtValor.setText(jsonWinthor[2]);
        txtSifrao.setVisibility(View.VISIBLE);
    }
}

Read JSON object:

{"Codigo":78,"Descricao":"BANDEJA PINT.TIGRE 1308","Preco":9.20,"CodigoBarra":"78"}
  • Young man, please post your code in text and not image.

  • Okay. Made a change.

1 answer

0


You can first convert to double and to use the format. See:

double value = Double.parseDouble(jsonWinthor[2]);
String strValue = String.format("%.2f", value ); 

Behold in those answers other ways to do this.

  • 1

    Thanks! It worked perfectly. Thanks! Thanks.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.