Decimal places

decimal places are off in my code

Page 1 of 1

8 Replies - 1539 Views - Last Post: 01 August 2009 - 11:22 AM Rate Topic: -----

#1 sarah624  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 24-July 09

Decimal places

Post icon  Posted 24 July 2009 - 10:36 AM

I have had a devil of a time getting the calculations correct and now have the issue of the decimal places being off. Could someone give me a push in the right direction to fix this please. The code compiles and executes properly with the correct totals just not rounding and I am not sure how to fix this. Thanks

//Filename Mortgage Program McBride Financial SR-mf-003
//Written by <Sarah Hazen>
//Version 1.0 July 8, 2009

/*This program will calculate mortgage payment amounts that will calculate and display the monthly payment amount to fully amortize a $200,000.00 loan */

import java.util.*;
import java.text.NumberFormat;

public class MortgageCalc
{

public static void main(String[] args)
	{
	System.out.println("MORTGAGE CALCULATOR-MCBRIDE FINANCIAL SERVICES ver1.0");
	System.out.println("Written by Sarah Hazen PRG420");
	System.out.println("July 6, 2009");
	System.out.println();

//Declaring variables
	int loanAmt=200000;
	double interestRate=.0575;
	double interestTotal=loanAmt*interestRate;
	double totalAmt=loanAmt + interestTotal;
	double termYears=30*12;
	double monthlyPayment=totalAmt/termYears;
	double newBalance=totalAmt-monthlyPayment;

	NumberFormat moneyFormat = NumberFormat.getCurrencyInstance();

//Print calculator outputs
	System.out.println();
	System.out.println(" Loan Amount: $200,000");
	System.out.println(" Interest Rate = 5.75%");
	System.out.println(" Loan Term in months="+termYears);
	System.out.println(" Total interest = $" +interestTotal);
	System.out.println(" Total Loan Amount = $" +totalAmt);

//calculate monthly payment
	monthlyPayment = (loanAmt*(interestRate/12))/(1-1/Math.pow((1+interestRate/12),termYears));
	System.out.println(" Your monthly payment is:$"+monthlyPayment);

//declares new variables
	double loanBalance=0;
	double interestPaid=0;
	int lineCount=10;
	loanBalance=totalAmt-monthlyPayment;

//starts loop

	while (loanBalance >0)
	{

	System.out.println("The loan balance is: $"+loanBalance);
	System.out.println("The interest paid on the loan is: $"+interestPaid);

	loanBalance = loanBalance-monthlyPayment;
	interestPaid=monthlyPayment * interestTotal;

//pauses screen
	if (lineCount >0)
	{

	lineCount--;
	try {
		Thread.sleep(1500);
	}catch (InterruptedException e){
	}
	}
	}
//stops loop statement
	if (loanBalance <=0)
	{
		System.out.println("The loan balance is: $0.00");
	}

	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Decimal places

#2 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Decimal places

Posted 24 July 2009 - 10:39 AM

use a printf statement. example
System.out.printf("The number is: %.2d", 12.45654);
or
Use a DecimalFormatter

Hope this helps!
Was This Post Helpful? 1
  • +
  • -

#3 sarah624  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 24-July 09

Re: Decimal places

Posted 24 July 2009 - 02:31 PM

View PostFuzzyness, on 24 Jul, 2009 - 09:39 AM, said:

use a printf statement. example
System.out.printf("The number is: %.2d", 12.45654);
or
Use a DecimalFormatter

Hope this helps!


Thank you I will try the print statement and if that doesn't work I will attempt to use a decimalformat. I will let you know if I get it to work.
Was This Post Helpful? 0
  • +
  • -

#4 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Decimal places

Posted 24 July 2009 - 02:49 PM

I cannot remember if it is %.2d which I believe is for doubles.

you need to change it to %.2f

This will help.


Happy Coding!
Was This Post Helpful? 0
  • +
  • -

#5 sarah624  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 24-July 09

Re: Decimal places

Posted 24 July 2009 - 03:03 PM

View PostFuzzyness, on 24 Jul, 2009 - 01:49 PM, said:

I cannot remember if it is %.2d which I believe is for doubles.

you need to change it to %.2f

This will help.


Happy Coding!


That was the problem the "d" didnt work so I'll change it to %.2f. Thanks again.
Was This Post Helpful? 0
  • +
  • -

#6 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Decimal places

Posted 24 July 2009 - 03:05 PM

No problem, just go to that link that I gave you. You can learn about all the formatting options using printf. I would also look into the DecimalFormatter Class, you may just need to format a double but not print it.

Happy Coding! :)
Was This Post Helpful? 1
  • +
  • -

#7 sarah624  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 24-July 09

Re: Decimal places

Posted 01 August 2009 - 07:35 AM

View PostFuzzyness, on 24 Jul, 2009 - 02:05 PM, said:

No problem, just go to that link that I gave you. You can learn about all the formatting options using printf. I would also look into the DecimalFormatter Class, you may just need to format a double but not print it.

Happy Coding! :)


I figured out the decimal places and started on the third portion of the assignment where I need a loop to calculate the three different terms/rates and calculate the payments until the balance owed is $0. This is where I am running into errors on line 68 the compiler is saying that instance is expected System.out.println(); before the parentheses. Could someone look at my program and explain what this means. Thanks in advance for any help.
/* Mortgage Calculator containing an array for 3 different interest rates and 3 separate lengths for term.
By Sarah Hazen
PRG420 Java Programming
July 27, 2009 */

import java.io.*;
import java.text.*;

public class CalcCR3sh
{
	public static void main (String[] args) throws IOException
	{
		System.out.println("Mortgage Calculator");
		System.out.println("Sarah Hazen");
		System.out.println("PRG 420 Java Programming");
		System.out.println("July 27th, 2009");
		System.out.println();


/*declaring output variables*/

		DecimalFormat twoplaces = new DecimalFormat ("#,###.00");
		InputStreamReader reader = new InputStreamReader (System.in);
		BufferedReader object = new BufferedReader (reader);



/*Array-assigning variables for loan balance at 7,15,30 year terms with varying interest rates for each term*/
			double loan = 200000.00;
			int[] length = new int [3];
						length[0]=7;
						length[1]=15;
						length[2]=30;

			double [] interest = new double [3];
						interest[0] = .0535;
						interest[1] = .0550;
						interest[2] = .0575;
			int x = 0;

	do
	{
			double payment = (loan*(interest[x]/12))/(1-(Math.pow(1/(1+(interest[x]/12)),(length[x]*12))));

			System.out.println();
			System.out.println("Loan Principle Amount=$200,000.00");
			System.out.println("Your Mortgage Payment: $" +twoplaces.format (payment)+" for the " +length[x]+ "-Year Contract.");


			x++;

	}
	while (x<3);


	for (int i = 0; i<=11; i++)
			interestPay = amount * interest;
			amount -= (total - interestPay);

			System.out.println();
			System.out.print("Payment # " +counter++);
			System.out.print("\tPrinciple Balance: "+moneyFormat.format(amount));
			System.out.print("\tMonth Interest: "+moneyFormat.format(interest));
			System.out.println();
			System.out.print("Press y and hit enter ...");

	}
			System.out.println();
			System.out.print("Press y and hit enter to continue...");
			acctNum = scan.next();
	}

			System.out.println();
			System.out.print("Next the loan payment for a "+length[2]);
			System.out.println(" year loan at "+interest[2] * 100+" percent.");

/*Start Array Location 2 and declare array variables*/

			length=length[2] * 12;
			interest=interest[2] / 12;
			amount=200000;

			total = amount * ( interest / (1 - Math.pow((1 + interest), -length)));



			System.out.println();
			System.out.println("Loan Amount: "+moneyFormat.format(amount));
			System.out.println("Interest Rate: "+interest[2]*100+" percent");
			System.out.println("Length of Loan: "+length[2]+" years");
			System.out.println("Total Monthly Payment: "+moneyFormat.format(total));
			System.out.println();
			System.out.print("Press y and hit enter ...");


		counter=1;


		while (amount >= 1)
	{

/* loop statement to show 12 lines at a time*/

		for (int i = 0; i<=11; i++)
	{

/* payment output*/

			interestPay = amount * interest;
			amount -= (total - interest);


			System.out.println();
			System.out.print("Payment # " +counter++);
			System.out.print("\tPrinciple Balance: "+moneyFormat.format(amount));
			System.out.print("\tMonth Interest: "+moneyFormat.format(interest));
			System.out.println();

	}


			System.out.println();
			System.out.print("Press y and hit enter ...");


	}

			System.out.println();
			System.out.println("Congratulations!!!You are now the owner of your home");
			System.out.println();



	}

}


Was This Post Helpful? 0
  • +
  • -

#8 Atspulgs  Icon User is offline

  • D.I.C Regular

Reputation: 68
  • View blog
  • Posts: 379
  • Joined: 29-July 09

Re: Decimal places

Posted 01 August 2009 - 09:44 AM

As far as i got the time to look trough i found some { to many or even not enough.

Just look trough your { curly braces. Ill look trough it with grater care when i got more time. In case its not solved yet.
Was This Post Helpful? 0
  • +
  • -

#9 sarah624  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 24-July 09

Re: Decimal places

Posted 01 August 2009 - 11:22 AM

View PostAtspulgs, on 1 Aug, 2009 - 08:44 AM, said:

As far as i got the time to look trough i found some { to many or even not enough.

Just look trough your { curly braces. Ill look trough it with grater care when i got more time. In case its not solved yet.


Thanks I will go over it again to make sure that all the opening braces have closing ones to match. I will get back to you and let you know what I find. Thanks again.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1