得到GTotal的值,舉例: 179.257856
但是要求只顯示小數點後兩位,也就是這樣: 179.25
@Override
protected void onResume()
{
super.onResume();
if (Constants.mItem_Detail.size() == 0)
{
return;
}
Double mGTotal = Double.parseDouble(Constants.mItem_Detail.get(0).get(SingleProductActivity.KEY_TOTAL));
for (int i = 1; i < Constants.mItem_Detail.size(); i++)
{
mGTotal = mGTotal + Double.parseDouble(Constants.mItem_Detail.get(i).get(SingleProductActivity.KEY_TOTAL));
}
mGrandTotal = String.valueOf(mGTotal);
mTxtViewGrandTotal.setText(mGrandTotal);
myTextVeiwGrandTotal.setText(mGrandTotal);
mTotal = String.valueOf(Constants.mItem_Detail.size());
mItems.setText(mTotal);
}
}
你已經完成的差不多了,我想加上下面的代碼就行了。
mTxtViewGrandTotal.setText(new DecimalFormat("##.##").format(mGTotal));
完整的方法就是:
@Override
protected void onResume()
{
super.onResume();
if (Constants.mItem_Detail.size() == 0)
{
return;
}
Double mGTotal = Double.parseDouble(Constants.mItem_Detail.get(0).get(SingleProductActivity.KEY_TOTAL));
for (int i = 1; i < Constants.mItem_Detail.size(); i++)
{
mGTotal = mGTotal + Double.parseDouble(Constants.mItem_Detail.get(i).get(SingleProductActivity.KEY_TOTAL));
}
mGrandTotal = String.valueOf(mGTotal);
mTxtViewGrandTotal.setText(mGrandTotal);
myTextVeiwGrandTotal.setText(mGrandTotal);
mTxtViewGrandTotal.setText(new DecimalFormat("##.##").format(mGTotal));
mTotal = String.valueOf(Constants.mItem_Detail.size());
mItems.setText(mTotal);
}