2

你如何格式化输出?

我的代码是:

package gradplanner;

import java.util.Scanner;

public class GradPlanner {

int cuToComp;
int cuPerTerm;

public static void main(String[] args) {

    final double COST = 2890.00; //flat-rate tuition rate charged per term
    final int MONPERTERM = 6; //number of months per term
    int cuToCompTotal = 0;   
    int numTerm;
    int numMonToComp;
    double tuition;



      //prompt for user to input the number of CUs for each individual course remaining.
    Scanner in = new Scanner(System.in);
    System.out.print("Please enter the number of CUs for each individual course you have remaining, Entering a - number when finished. ");     
    int cuToComp = in.nextInt();



      //add all CUs from individual courses to find the Total number of CUs left to complete.
    while (cuToComp > 0)
    {
      cuToCompTotal += cuToComp;

      System.out.print("Please enter the number of CUs for each individual course you have remaining, Entering a - number when finished. ");
      cuToComp = in.nextInt();
    }

    System.out.println("The total number of CUs left is " + cuToCompTotal);



      //prompt for user to input how many CUs they plan to take per term.
    System.out.print("How many credit units do you intend to take per term? ");
    int cuPerTerm = in.nextInt();

        if (cuPerTerm < 12) //validate input - Undergraduate Students Must enroll in a minimum of 12 CUs per term
        {
            System.out.print("Undergraduate Students must enroll in a Minimum of 12 CUs per Term. ");
                while(cuPerTerm < 12){
                    System.out.print("How many credit units do you intend to take per term? ");
                    cuPerTerm = in.nextInt();
                }
        }


        //Calculate the number of terms remaining, if a remain is present increase number of terms by 1.   
     numTerm = cuToCompTotal/cuPerTerm;
        if (cuToCompTotal%cuPerTerm > 0)
        {
          numTerm = numTerm + 1;  
        }
     System.out.println("The Number of Terms you have left is " + numTerm + " Terms. ");



       //Calculate the number of Months left to complete
     numMonToComp = numTerm * MONPERTERM;
     System.out.println("Which is " + numMonToComp + " Months. ");



       //calculate the tuition cost based on the number of terms left to complete.
     tuition = numTerm * COST;
     System.out.println("Your Total Tuition Cost is: " + "$" + tuition +" . ");

}

}

最后一行 System.out.println("你的总学费是:" + "$" + 学费 +" . ");
我需要对其进行格式化,使其具有两位小数(amount.00)以及占位符的逗号。

我试过了

System.out.printf("Your Total Tuition Cost is: " + "$%.2f" + tuition +" . ");

但是我得到一个错误!!!!

所以我加了

NumberFormat my = NumberFormat.getInstance(Locale.US);
     my.setMaximumFractionDigits(2);
     my.setMinimumFractionDigits(2);
     String str = my.format(tuition);
     System.out.printf("Your Total Tuition Cost is: $",    (NumberFormat.getInstance(Locale.US).format(tuition)));

哪个输出

Your Total Tuition Cost is: $BUILD SUCCESSFUL (total time: 13 seconds)

我的错误是什么???

也永远不会有十进制值,xx.00 将永远是 .00(这有关系吗?)

4

2 回答 2

4

最好的替代方法是使用NumberFormat类,当您想要自定义小数分隔符和占位符等内容时,它会更好。

由于您似乎正在寻找的输出是美国标准,您可以简单地使用这个:

NumberFormat my = NumberFormat.getInstance(Locale.US);
my.setMaximumFractionDigits(2);
my.setMinimumFractionDigits(2);
String str = my.format(123456.1234);

如果不是出于您的占位符要求,可以使用的另一种替代方法是使用String.format-method。

在许多情况下,一种简单的方法是使用该String.format方法,该行将输出如下123456.12

String.format("Your total cost is : $%.02f", tuition);
于 2013-10-21T22:39:03.453 回答
0

出于某种原因,这里的答案都没有提到货币格式:

System.out.println("Your Total Tuition Cost is: " + 
    NumberFormat.getCurrencyInstance().format(tuition));

您也可以使用 做同样的事情MessageFormat,您可能会发现它更容易使用,也可能不会:

System.out.println(
    MessageFormat.format("Your Total Tuition Cost is: {0,number,currency}",
        tuition));
于 2013-10-25T01:45:08.597 回答