4 Replies - 1888 Views - Last Post: 04 November 2008 - 05:17 PM Rate Topic: -----

#1 didi2005   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 15-September 08

Decimal Point

Posted 22 September 2008 - 07:13 PM

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



 
public class Q1a {
	public static void main (String args[]) throws IOException
	
	{
		
		float loan_amount, interest_rate, interest, principal, unpaid_amount, installment;
		float total_interest = 0;
		int loan_period, loan_period_month;
		int payment_no = 0;
		int a;
		Pembaca read;
		read = new Pembaca();
		
		
		System.out.print("Enter loan amount (RM):"); //Input of loan amount
		loan_amount = read.bacaFloat ();
		System.out.println("Loan amount is "+loan_amount);
		
			System.out.print("Enter annual interest rate(%):"); //Input of interest rate
			interest_rate = read.bacaFloat ();
			System.out.println("Interest rate is "+interest_rate);
			
		System.out.print("Enter loan period(year):"); //Input of loan period
		loan_period = read.bacaInt ();
		loan_period_month = loan_period * 12;
		System.out.println("Loan period is " +loan_period_month+ " months");
		
			System.out.print("Enter monthly installment amount:"); //Input of monthly payment
			installment = read.bacaFloat ();
			System.out.println("Monthly installment is RM" +installment);
		
		System.out.println(""); //spacing
		

			
		System.out.println(""); //spacing
			
			DecimalFormat twoDigits = new DecimalFormat(".00");		
   			System.out.println("Payment\t\t\t\t\tUnpaid\t\tTotal Interest"); //Output of table listing
			System.out.println("No.\tInterest\tPrincipal\tBalance\t\tTo Date\t");
		

				
				
				  
			
			for ( a = 1; a <= loan_period_month; a++){
				payment_no++;
				interest = loan_amount * loan_period_month / 100 / 12;
				principal = installment - interest;
				unpaid_amount = loan_amount - principal;
				total_interest += interest;
				System.out.println("" + payment_no + "\t" + interest + "\t\t" + principal + "\t\t" + unpaid_amount + "\t\t" + total_interest);
				loan_amount = unpaid_amount;

				
			}
				
			
				
			
				

		
	
		
				
		
			
			
	}
}



This is the outcome of my program.
Posted Image
How do i make the table values 2 decimal point only? Example, 41.96.
I've tried using "import java.text.DecimalFormat;" and "DecimalFormat twoDigits = new DecimalFormat("$##,###.##");" in my coding...

Is This A Good Question/Topic? 0
  • +

Replies To: Decimal Point

#2 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Decimal Point

Posted 22 September 2008 - 07:26 PM

nice to have created your DecimalFormat but you'll have to use it :)

		System.out.println("" + payment_no 
	   + "\t" + twoDigits.format(interest) + "\t\t" 
	   + twoDigits.format(principal)
	   + "\t\t" + twoDigits.format(unpaid_amount) 
	   + "\t\t" + twoDigits.format(total_interest));


Was This Post Helpful? 0
  • +
  • -

#3 didi2005   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 15-September 08

Re: Decimal Point

Posted 22 September 2008 - 08:01 PM

View Postpbl, on 22 Sep, 2008 - 07:26 PM, said:

nice to have created your DecimalFormat but you'll have to use it :)

		System.out.println("" + payment_no 
	   + "\t" + twoDigits.format(interest) + "\t\t" 
	   + twoDigits.format(principal)
	   + "\t\t" + twoDigits.format(unpaid_amount) 
	   + "\t\t" + twoDigits.format(total_interest));


Thx for your help, now i able to use it.

Posted Image
Was This Post Helpful? 0
  • +
  • -

#4 ida200   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 04-November 08

Re: Decimal Point

Posted 04 November 2008 - 12:50 AM

why in this program i try the read statement cannot fine. the syntax is symbol cannot resolve
Was This Post Helpful? 0
  • +
  • -

#5 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Decimal Point

Posted 04 November 2008 - 05:17 PM

View Postida200, on 3 Nov, 2008 - 11:50 PM, said:

why in this program i try the read statement cannot fine. the syntax is symbol cannot resolve

From Sept 22nd to today... what is the exact question ?
You have an error ?
Compile error ? What it is ?
Run time error ? What it is ?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1