当前位置: 动力学知识库 > 问答 > 编程问答 >

java - What is DecimalFormat and how should you use it?

问题描述:

I'm quite new to this and have been trying to figure out how DecimalFormat works. I'm not sure what part I'm doing wrong so I'll include the entire lot of code. I have two questions:

  1. Is DecimalFormat a method?
  2. How should I be using it to get this to work correctly, nothing I've found online has properly explained it, and just copying how they formatted it didn't work?

Code:

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?");

}

}

}

网友答案:

First of all, no DecimalFormat is not a method, it is a class under java.text.

Second of all, to use DecimalFormat, you would use

dollars.format

not

format.dollars

This means that your dollars variable needs to be declared outside of your if-statement so that it can be used in your "else" statement. See the revised code below:

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?");
      }
   }

So when I input "25" into this system it outputs:

That's 2 dozen at $3.25 per dozen, and 1 loose eggs at 45 cents each.

For a total of $6.95.

分享给朋友:
您可能感兴趣的文章:
随机阅读: