Java 如何将双变量四舍五入到小数点后2位?

Java 如何将双变量四舍五入到小数点后2位?,java,android,double,Java,Android,Double,至于运费和小计,我已申报为双倍。然而 它们只显示25.0或128.0的数值,只有一个小数点。我希望显示25.00或128.00。我应该如何修改我的代码 @Override protected void onResume() { super.onResume(); // Refresh the data if(mProductAdapter != null) { mProductAdapter.notifyDataSetChanged(); }

至于运费和小计,我已申报为双倍。然而 它们只显示25.0或128.0的数值,只有一个小数点。我希望显示25.00或128.00。我应该如何修改我的代码

@Override
protected void onResume() {
    super.onResume();

    // Refresh the data
    if(mProductAdapter != null) {
        mProductAdapter.notifyDataSetChanged();
    }

    double totalweight = 0;
    double shippingfee = 0;
    for(product p : mCartList) {
        int quantity = ShoppingCartActivity.getProductQuantity(p);
        totalweight += p.weight * quantity;
        shippingfee = ShoppingCartActivity.getShipping(totalweight);
    }

    double subTotal = 0;
    for(product p : mCartList) {
        int quantity = ShoppingCartActivity.getProductQuantity(p);
        subTotal += p.price * quantity;
    }
    if (subTotal >= 150){
        shippingfee = 0;
        subTotal += shippingfee;
    }
    else{
        for(product p : mCartList) {
            int quantity = ShoppingCartActivity.getProductQuantity(p);
            totalweight += p.weight * quantity;
            shippingfee = ShoppingCartActivity.getShipping(totalweight);
        }
        subTotal += shippingfee;
    }
    TextView productPriceTextView = (TextView) 
    findViewById(R.id.TextViewSubtotal);
    productPriceTextView.setText("Subtotal: RM" + subTotal);
    TextView productWeightTextView = (TextView) 
    findViewById(R.id.TextViewSubweight);
    productWeightTextView.setText("Shipping Weight: " + totalweight +" kg");
    TextView productShippingTextView = (TextView) 
    findViewById(R.id.TextViewShipping);
    productShippingTextView.setText("Shipping Fee: RM" + shippingfee);
}

如果您只想将其四舍五入为'00'值,可以将其格式化为'00'值

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

如果您只想将其四舍五入为'00'值,可以将其格式化为'00'值

String value = String.format("%.2f",price);
使用-

使用-

无论如何,你不应该使用。可能会更好。无论如何,你不应该使用。可能会更好。