结果显示无小数

有什么方法可以将这个计算结果放入没有小数点的价格结果中。


 private void calculate() {


    double b = Double.parseDouble(berat.getText().toString());


    if (b <= 0.5) {

        totalPrice1 = 7;

        serviceFee1 = totalPrice1 * 0.2;

        carierFee1 = totalPrice1 * 0.8;

    } else {

        totalPrice1 = b * 12.9;

        serviceFee1 = totalPrice1 * 0.2;

        carierFee1 = totalPrice1 * 0.8;

    }


}

例如,如果我添加 0.5,则价格结果将在承运人费用中为 5.60000000000。我希望它显示为 5.60。


慕婉清6462132
浏览 141回答 3
3回答

开心每一天1111

更新您的功能如下:&nbsp;private void calculate() {&nbsp; &nbsp; double b = Double.parseDouble(berat.getText().toString());&nbsp; &nbsp; if (b <= 0.5) {&nbsp; &nbsp; &nbsp; &nbsp; totalPrice1 = 7;&nbsp; &nbsp; &nbsp; &nbsp; serviceFee1 = totalPrice1 * 0.2;&nbsp; &nbsp; &nbsp; &nbsp; carierFee1 = totalPrice1 * 0.8;&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; totalPrice1 = b * 12.9;&nbsp; &nbsp; &nbsp; &nbsp; serviceFee1 = totalPrice1 * 0.2;&nbsp; &nbsp; &nbsp; &nbsp; carierFee1 = totalPrice1 * 0.8;&nbsp; &nbsp; }&nbsp; &nbsp; serviceFee1 = Math.round(serviceFee1 * 100.0) / 100.0;&nbsp; &nbsp; carierFee1 = Math.round(carierFee1 * 100.0) / 100.0;}

DIEA

试试这个DecimalFormat form = new DecimalFormat("0.00");&nbsp; &nbsp; &nbsp; &nbsp; String formatedStr = form.format(carrierfee);

拉风的咖菲猫

有两种显示小数的方法。DecimalFormat df = new DecimalFormat("#.00");&nbsp;字符串结果 = df.format(carierFee1);结果将是:价格=0.5 -> 结果=5.6String result = String.format( "%.2f", carierFee1 );结果将是:价格=0.5 -> 结果=5.60您可以根据自己的目的选择一种。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java