什么是DecimalFormat以及您应该如何使用它



我对此很陌生,一直在试图弄清楚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: $3.25 LOOSE EGGS: $0.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 $3.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("$0.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 下的一个类。

其次,要使用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: $3.25 LOOSE EGGS: $0.45");
      int eggsInput = inputDevice.nextInt();
      int eggsDozen = eggsInput / 12;
      int eggsLoose = eggsInput % 12;
      boolean isDozen = eggsDozen >= 1;
      DecimalFormat dollars = new DecimalFormat("$0.00"); //now this format can be used throughout the main method
      if (isDozen == true) {
      System.out.println("That's " + eggsDozen + " dozen at $3.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 美元。

相关内容

  • 没有找到相关文章

最新更新