如何使用这个简单的代码制作2位十进制答案?

时间:2015-08-21 11:31:49

标签: javascript java double currency

public class TaskFive {

   public static void main(String[] args) {

      int cheques=5;
      double baseFee=10.0; 
      double bankCharge,costCheque;

      if (cheques<20){

         costCheque=0.1; // 

      }else if ((cheques>20) && (cheques<39)) {

         costCheque=0.08;

      }else if ((cheques>40) && (cheques<59)) {

         costCheque=.06;

      }else{

         costCheque=0.04;

      }

      bankCharge=baseFee+(costCheque*cheques); //dont mind this… just a catch my lecturer added

      System.out.println("____________________________________________"+"\nBank Charge:P"+bankCharge+"\t Base Fee:P"+baseFee+"\n Cost of a Cheque:P"+costCheque+"\t Number of Cheques:"+cheques+"\n____________________________________________");

   }
}

当我需要10.50等结果时,这里的结果是10.5和10.0

1 个答案:

答案 0 :(得分:0)

使用new DecimalFormat("##.##").format(bankCharge)

它会将它转换为小数后的2位数