2016-07-29 124 views
-2

我想讓下面的代碼只顯示四捨五入的小數點後兩位數。 。 。圓整數>小數點後兩位

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

    } 

} 
+2

看https://docs.oracle.com/javase/7/docs/api/java/text/NumberFormat.html –

+1

使用'printf()',如下所示:'System.out.printf(「Gross Refund£%.2f%n」,gross);' – Andreas

回答

-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)); 
} 
} 
相關問題