Ben Fogler
Ben Fogler

Reputation: 35

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

I currently have a code that works properly (compiled and tested multiple times) however I am outputting:

Card Balance: $500.0
Minimum payment to principle (3% of principle): $15.0
Minimum total payment (payment and fees): $60.0

and I need these values to print out with 2 decimals instead of 1 (500.00 instead of 500.0). I know this is petty and simple, but no other forum has helped me thus far - when I try printf I get a conversion error, when I try DecimalFormat I get an error that DecimalFormat does not exist. I'm just at a loss and would appreciate any help. I will include all relevant code below (but for relevance issues I won't add all the rest of the code).

    //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);

Upvotes: 3

Views: 790

Answers (3)

Ashish Rajvanshi
Ashish Rajvanshi

Reputation: 466

The java.io package includes a PrintStream class that has two formatting methods that you can use to replace print and println. These methods, format and printf, are equivalent to one another. The familiar System.out that you have been using happens to be a PrintStream object, so you can invoke PrintStream methods on System.out. Thus, you can use format or printf anywhere in your code.

The following program shows some of the formatting that you can do with format. The output is shown within double quotes in the embedded comment:

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"
    }
}

For more detail you can check https://docs.oracle.com/javase/tutorial/java/data/numberformat.html

Upvotes: 1

RYS221
RYS221

Reputation: 50

printf("Card Balance: $%.2f\n", bal);

When using printf, you use %{s,d,f,etc} to inform print what kind of variables it will be printing eg. s for string, d for int etc. The .2 specifies 2 decimals. \n will have the same effect as println making it go to the next line. Technically you format the entire string within the " ". Also with printf you have to use , to separate the different parameters as opposed to the + used in println

Similarly:

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

EDIT:

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

Where we have to use double %% to indicate we want to print the % and it is not part of the formatting

Upvotes: 1

Abdelhak
Abdelhak

Reputation: 8387

You can use the printf method, like so:

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

In short, the %.2f syntax tells Java to return your variable (bal) with 2 decimal places.

Upvotes: 2

Related Questions