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

标签 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(这重要吗?)

最佳答案

最好的替代方法是使用 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);

关于java - 输出格式打印带有两位小数和占位符的逗号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19506071/

相关文章:

java - 删除文件时出现问题

java - 绘图 Canvas 上的二维数组网格

java - 为什么 AbstractList 的设计使得某些方法总是抛出 UnsupportedException?

java - Silvertunnel异常: java. lang.NoSuchMethodError : org. bouncycaSTLe.asn1.ASN1InputStream.readObject()Lorg/bouncycaSTLe/asn1/DERObject;

Java - 对象列表返回重复值

java - ReentrantLock如何同步?

java - Docx4j - 更改 docDefaultStyle 的语言设置/将自定义(或更改的)库存样式正确应用到 docx 中的任何文本

java - Java转储中retained size=0是什么意思?

java - JTextArea 自动换行调整大小

Java swing组件显示图像网格