输出格式打印带有两个小数位和逗号占位符

时间:2013-10-21 22:37:46

标签: java

如何格式化输出?

我的代码是:

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(这有关系吗?)

2 个答案:

答案 0 :(得分:4)

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

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

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

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

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

String.format("Your total cost is : $%.02f", tuition);

答案 1 :(得分: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));