String JSON - Two decimal places

1

I'm reading a JSON object, and I get the data in TextView . However, the value of String Preco when the price has a zero in the second decimal place, it does not print in TextView . For example: 9.20 displays 9.2 . I tried some functions, but to no avail. 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);
    }
}

JSON object read:

{"Codigo":78,"Descricao":"BANDEJA PINT.TIGRE 1308","Preco":9.20,"CodigoBarra":"78"}
    
asked by anonymous 22.08.2017 / 21:24

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 ); 

See in this answers other ways to do this.

    
22.08.2017 / 22:02