1

我得到 mGTotal 的价值,例如: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);

}
}
4

2 回答 2

7

采用Decimalformat

DecimalFormat df = new DecimalFormat("#.##");
System.out.println(df.format("179.257856"));// which gives result as 179.25
于 2013-02-20T05:39:24.483 回答
3

你非常接近你的答案,我想下面的行对你有用:

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

}
于 2013-02-20T05:46:05.703 回答