什么是 DecimalFormat,您应该如何使用它?

What is DecimalFormat and how should you use it?

我对此很陌生,一直在努力弄清楚 DecimalFormat 的工作原理。我不确定我做错了什么部分,所以我将包含全部代码。我有两个问题:

  1. DecimalFormat 是一种方法吗?
  2. 我应该如何使用它才能使它正常工作,我在网上找到的任何内容都没有正确解释它,只是复制它们的格式化方式不起作用?

代码:

import java.util.Scanner;
import java.text.DecimalFormat;
public class Eggs
{
   public static void main(String[] args)
   {
      // We'll initialise some constants for the price of a dozen eggs and a loose egg. We'll also initialise the input device.
      final float PRICEDOZEN = 3.25f;
      final float PRICELOOSE = .45f;
      Scanner inputDevice = new Scanner(System.in);
      System.out.println("How many eggs are you ordering? \nPRICELIST: \nONE DOZEN EGGS: .25 LOOSE EGGS: [=10=].45");
      int eggsInput = inputDevice.nextInt();
      // Here we'll initialise some variables for storing the number of dozens of eggs ordered, the number of loose eggs, and whether there 
      // is at least one dozen. We use these variables to calculate the price, and we'll also use them to clean up the output text.
      int eggsDozen = eggsInput / 12;
      int eggsLoose = eggsInput % 12;
      boolean isDozen = eggsDozen >= 1;

      // Now we'll have an if else statement, if there is at least one dozen we'll mention that in the output text, if not, we'll have different text.
      if (isDozen == true) {
      System.out.println("That's " + eggsDozen + " dozen at .25 per dozen, and " + eggsLoose + " loose eggs at 45 cents each.");
      // I had a problem where the '+' operator was being used for concatenation instead of arithmetic. I guessed that if you enclosed the arithmetic
      // in paranthesis (x * y + a *b) it would ensure that the arithmetic would be performed before concatenation. I was right.
      // I believe this is because concaternation is still subject to the rules of Order of Operations, so the paranthesis is performed before the addition
      // of concaternation. 

      // I've looked up online and apparently you can use "DecimalFormat x = new DecimalFormat("#.##") to format decimal numbers. I'll try it out.
      DecimalFormat dollars = new DecimalFormat("[=10=].00");
      System.out.println("For a total of $" + (eggsDozen * PRICEDOZEN + eggsLoose * PRICELOOSE) + ".");
      }
      else {
      System.out.println("That's " + eggsLoose + " loose eggs at 45 cents each.");
      System.out.println("For a total of " + (format.dollars(eggsLoose * PRICELOOSE)) + ". Did you know we offer a discount on eggs ordered by the dozen?");
      }
   }
}

首先,没有DecimalFormat不是一个方法,它是java.text下的一个class。

其次,要使用 DecimalFormat,您将使用

dollars.format

不是

format.dollars

这意味着您的美元变量需要在您的 if 语句之外声明,以便它可以在您的 "else" 语句中使用。请参阅下面修改后的代码:

public static void main(String[] args)
   {

      final float PRICEDOZEN = 3.25f;
      final float PRICELOOSE = .45f;
      Scanner inputDevice = new Scanner(System.in);
      System.out.println("How many eggs are you ordering? \nPRICELIST: \nONE DOZEN EGGS: .25 LOOSE EGGS: [=12=].45");
      int eggsInput = inputDevice.nextInt();

      int eggsDozen = eggsInput / 12;
      int eggsLoose = eggsInput % 12;
      boolean isDozen = eggsDozen >= 1;

      DecimalFormat dollars = new DecimalFormat("[=12=].00"); //now this format can be used throughout the main method

      if (isDozen == true) {
      System.out.println("That's " + eggsDozen + " dozen at .25 per dozen, and " + eggsLoose + " loose eggs at 45 cents each.");

      System.out.println("For a total of $" + (eggsDozen * PRICEDOZEN + eggsLoose * PRICELOOSE) + ".");
      }
      else {
      System.out.println("That's " + eggsLoose + " loose eggs at 45 cents each.");
      System.out.println("For a total of " + (dollars.format(eggsLoose * PRICELOOSE)) + ". Did you know we offer a discount on eggs ordered by the dozen?");
      }
   }

所以当我在这个系统中输入“25”时,它输出:

2 打,每打 3.25 美元,1 个散装鸡蛋,每个 45 美分。

总计 6.95 美元。