3

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);
Ben Fogler
  • 35
  • 4
  • Check this out http://stackoverflow.com/questions/8819842/best-way-to-format-a-double-value-to-2-decimal-places – Log Feb 09 '16 at 05:54

3 Answers3

2

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.

Abdelhak
  • 8,299
  • 4
  • 22
  • 36
1
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

RYS221
  • 60
  • 9
  • 1
    Definitely helped! but when I put in: 'printf("Minimum payment to principle (3% of principle): $%.2f\n", minPay);' instead of: 'System.out.println("Minimum payment to principle (3% of principle): $" + (minPay));' I get error: – Ben Fogler Feb 09 '16 at 06:38
  • Minimum payment to principle (3Exception in thread "main" java.util.IllegalFormatConversionException: o != java.lang.Double at java.util.Formatter$FormatSpecifier.failConversion(Unknown Source) at java.util.Formatter$FormatSpecifier.printInteger(Unknown Source) at java.util.Formatter$FormatSpecifier.print(Unknown Source) at java.util.Formatter.format(Unknown Source) at java.io.PrintStream.format(Unknown Source) at java.io.PrintStream.printf(Unknown Source) at Program3.main(Program3.java:106) – Ben Fogler Feb 09 '16 at 06:42
  • 1
    Oh. It is assuming the (%3 ...) is a formatter, which it obviously isn't. To print a % sign, use %%. This indicates its to be printed and not part of the formatting. As in: printf("Minimum payment to principle (3%% of principle): $%.2f\n", minPay); That should work. – RYS221 Feb 09 '16 at 06:44
  • You are literally a life saver!!! That makes so much sense!!! thank you! That was the final touch on that program! The longest one I've written yet! Thank you @RYS221 ^_^ – Ben Fogler Feb 09 '16 at 06:53
1

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

Ashish Rajvanshi
  • 466
  • 4
  • 15