Java – How to display only two decimal places

How to display only two decimal places… here is a solution to the problem.

How to display only two decimal places

I

just want to show two digits after the decimal point, I tried but I still get a lot of digits after the decimal point :

itemamount = Double.parseDouble(text_cost_code.getText().toString());
txt_total.setText(new DecimalFormat("##.##").format(itemamount));

edit_qty_code.addTextChangedListener(new TextWatcher() {

public void onTextChanged(CharSequence s, int start, int before,
        int count) {
 TODO Auto-generated method stub
if (!edit_qty_code.getText().toString().equals("")
|| !edit_qty_code.getText().toString().equals("")) {
itemquantity = Double.parseDouble(edit_qty_code.getText().toString());
itemamount = Double.parseDouble(text_cost_code.getText().toString());
txt_total.setText(new DecimalFormat("##.##").format (itemquantity * itemamount));
} else { txt_total.setText("0.00"); }}

Solution

I’m not sure where in the code you want to do this, but

String.format("%.2f", value);

It should be ok.

Related Problems and Solutions