圆数>两位小数

时间:2016-07-29 15:41:00

标签: java

我希望以下代码仅显示两位小数的舍入数字。 。 。

public class Main {

    public static void main(String[] args) {

        double gross = 4322.12d;
        double tax = 397.04d;

        double net = gross - tax;
        double fee = gross * 0.30d;
        double vat = fee * 0.20d;
        double takeHome = net - (fee + vat);

        System.out.println("Gross Refund £" + gross);
        System.out.println("Tax Deducted £" + tax);
        System.out.println("Net Refund £" + net);
        System.out.println("Our Fee £" + (fee + vat));
        System.out.println("Funds remaining after deductions £" + takeHome);

    }

}

1 个答案:

答案 0 :(得分:-1)

您的代码中的某些更改会检查这一点:

import java.text.DecimalFormat;

class TestClass {
public static void main(String[] args) {

double gross = 4322.12d;
double tax = 397.04d;

double net = gross - tax;
double fee = gross * 0.30d;
double vat = fee * 0.20d;
double takeHome = net - (fee + vat);
DecimalFormat df = new DecimalFormat("#.##");
System.out.println("Gross Refund :" + df.format(gross));
System.out.println("Tax Deducted :" + df.format(tax));
System.out.println("Net Refund :" + df.format(net));
System.out.println("Our Fee :" + df.format(fee + vat));
System.out.println("Funds remaining after deductions :" +df.format(takeHome));
}
}