需要在 java 中打印 2 个小数位而不是 1 个(特定实例)

Need to print with 2 decimal spaces instead of 1 (particular instance) in java

我目前有一个代码可以正常工作(多次编译和测试)但是我正在输出:

Card Balance: 0.0
Minimum payment to principle (3% of principle): .0
Minimum total payment (payment and fees): .0

我需要这些值以 2 位小数而不是 1 位小数打印出来(500.00 而不是 500.0)。我知道这很简单,但到目前为止没有其他论坛帮助过我 - 当我尝试 printf 时出现转换错误,当我尝试 DecimalFormat 时出现 DecimalFormat 不存在的错误。我只是不知所措,将不胜感激任何帮助。我将在下面包含所有相关代码(但由于相关性问题,我不会添加所有其余代码)。

    //Declaration
    double bal = 0;
    double intrst = 0;

    //Captures input
    Scanner scan = new Scanner(System.in);
    System.out.print("Current Balance: ");
    bal = scan.nextDouble();

     //Just explains intrst value
    if(late == true)
    if(lvl.equalsIgnoreCase(royal)) intrst = .022;
    else if(lvl.equalsIgnoreCase(gentry)) intrst = .028;
    else if(lvl.equalsIgnoreCase(common)) intrst = .03;
    else
   {
    System.out.println();
    System.out.print("Unknown customer level ");
    System.out.print("\"" + lvl + "\"");
    System.exit(1);
   }

    double minPay = (bal * 0.03);
    double totalMinPay = (minPay + lateFee) + (intrst * bal);

    System.out.println("Card Balance: $" +  bal);
    System.out.println("Minimum payment to principle (3% of principle): $" + (minPay));
    System.out.println("Minimum total payment (payment and fees): $" + totalMinPay);
printf("Card Balance: $%.2f\n", bal);

当使用 printf 时,您使用 %{s,d,f,etc} 来通知 print 它将打印什么样的变量,例如。 s 代表字符串,d 代表 int 等。.2 指定 2 位小数。 \n 将与 println 具有相同的效果,使其转到下一行。 从技术上讲,您可以在“”中格式化整个字符串。 同样对于 printf,您必须使用 , 来分隔不同的参数,而不是 println

中使用的 +

同样:

printf("Minimum payment to principle (3% of principle): $%.2f\n", minPay);

编辑:

printf("Minimum payment to principle (3%% of principle): $%.2f\n", minPay);

我们必须使用双 %% 来表示我们要打印 % 而它不是格式的一部分

你可以使用printf方法,像这样:

 System.out.printf("%.2f", bal);

简而言之,%.2f 语法告诉 Java 到 return 你的变量 (bal) 有 2 个小数位。

java.io 软件包包括一个 PrintStream class,它有两种格式化方法,您可以使用它们来替换 print 和 println。 format 和 printf 这两个方法是等价的。您一直在使用的熟悉的 System.out 恰好是一个 PrintStream 对象,因此您可以在 System.out 上调用 PrintStream 方法。因此,您可以在代码中的任何位置使用 format 或 printf。

以下程序显示了您可以使用 format 进行的一些格式化。输出显示在嵌入注释的双引号内:

import java.util.Calendar;
import java.util.Locale;

public class TestFormat {

    public static void main(String[] args) {
      long n = 461012;
      System.out.format("%d%n", n);      //  -->  "461012"
      System.out.format("%08d%n", n);    //  -->  "00461012"
      System.out.format("%+8d%n", n);    //  -->  " +461012"
      System.out.format("%,8d%n", n);    // -->  " 461,012"
      System.out.format("%+,8d%n%n", n); //  -->  "+461,012"

      double pi = Math.PI;

      System.out.format("%f%n", pi);       // -->  "3.141593"
      System.out.format("%.3f%n", pi);     // -->  "3.142"
      System.out.format("%10.3f%n", pi);   // -->  "     3.142"
      System.out.format("%-10.3f%n", pi);  // -->  "3.142"
      System.out.format(Locale.FRANCE,
                        "%-10.4f%n%n", pi); // -->  "3,1416"

      Calendar c = Calendar.getInstance();
      System.out.format("%tB %te, %tY%n", c, c, c); // -->  "May 29, 2006"

      System.out.format("%tl:%tM %tp%n", c, c, c);  // -->  "2:34 am"

      System.out.format("%tD%n", c);    // -->  "05/29/06"
    }
}

更多详情您可以查看 https://docs.oracle.com/javase/tutorial/java/data/numberformat.html