0

I am new to Java and I am coding a credit card Java assignment where you add charges to a credit card and add the interest with the charge. I coded an interest using the If statement but it outputs to a decimal that is not rounded.

public class Credit_Card
{
    public static void main (String []args)
    {
        Scanner c = new Scanner (System.in);
        double num1, num2, interest, newBalance, min = 0;

        int num3 = 50;

        System.out.println ("What is the previous balance?");
        num1 = c.nextInt();

        System.out.println ("What is the total amount of additional       charges?");
        num2 = c.nextInt();

        interest = num2;

        if (num2 == 0)
         interest = (interest + 0); //There is no interest if num2 is 0

        if (num2 > 0)
        interest = (interest*(2.0f/100.0f)); //total amount of money owed is multiplied by 2 percent

        newBalance = num1 + num2 + interest;

        System.out.println ("CS CARD International Statement");
        System.out.println ("===============================");
        System.out.println ("");
        System.out.println ("Previous Balance:      $" + num1 );
        System.out.println ("Additional Charges:    $" + num2 );
        System.out.println ("Interest:              $" + interest );
        System.out.println ("");
        System.out.println ("New Balance:           $" + newBalance);
        System.out.println ("");

        if (newBalance < 50)
         min = newBalance;

        if(newBalance >= 50 && newBalance <= 300)
         min = num3;

        if(newBalance > 301)
         min = (newBalance*(20.0f/100.0f));

        System.out.println ("Minimum Payment:       $" + min);
    }
}

The output look like this:

What is the previous balance?
1000
What is the total amount of additional charges?
25
CS CARD International Statement
===============================

Previous Balance:      $1000.0
Additional Charges:    $25.0
Interest:              $0.4999999888241291

New Balance:           $1025.4999999888241

Minimum Payment:       $205.100003053993

I think that because the interest is an everlasting decimal, it screws up everything else. Instead of the Interest being "0.49999999999", I want it to like like "0.50". Thank you very much in advance for any help.

Learning_Java
  • 65
  • 1
  • 4
  • 12
  • 1
    Have a look [here](http://stackoverflow.com/questions/153724/how-to-round-a-number-to-n-decimal-places-in-java) – blackpen Oct 06 '16 at 00:23
  • `if` statements don't have anything to do with rounding. You should never calculate money with floating-point. – user207421 Oct 06 '16 at 00:31

1 Answers1

3

String.format ("%.2f", var); where var is your newBalance, etc.

This rounds it to 2 decimal place.

Check out https://www.dotnetperls.com/format-java for a list of what you can do with formatter.

Zhi Kai
  • 1,549
  • 1
  • 13
  • 31
  • I am a little confused on where you would insert "String.format". Would you replace it with: interest = (interest*(2.0f/100.0f)); – Learning_Java Oct 06 '16 at 00:32
  • 1
    Inside your println. – Zhi Kai Oct 06 '16 at 00:33
  • For this: "%.2f", Is that exactly what I put in or do I replace "%" with the percentage? And since the Interest is not rounded, would I place it in the "var" instead of "newBalance? Sorry for my misunderstanding and thank you for your help – Learning_Java Oct 06 '16 at 00:36
  • 1
    Based on your code, `System.out.println ("Interest: $" + String.format ("%.2f", interest);`. Note that `%` is a character used to indicate that the following are the formatting instructions. – Zhi Kai Oct 06 '16 at 00:38
  • OK that makes more sense, I understand it now! Thank you very much! – Learning_Java Oct 06 '16 at 00:40
  • 1
    Glad to help. Do check out the formatter documentation on a range of formatting you can do. Alternatively, `DecimalFormat` is another class you can use to format decimal number. – Zhi Kai Oct 06 '16 at 00:42