Results 1 to 5 of 5
- 01-16-2009, 01:30 PM #1
Member
- Join Date
- Oct 2008
- Posts
- 39
- Rep Power
- 0
round down to 2 sig. numbers after the decimal point
I would like to round my number down to 2 sig. numbers after the decimal point in my answer, ie:
Old loan: £745.09
Loan plus 3% interest: £767.44
Wages: £19,206.92
Wages minus threshold: £9,206.92
Repayment: £828.62
New loan amount: £-61.18
at the moment my answer is:
Loan plus 3% interest: 767.4417386951634
wages: 19206.920769257813
Wages minus threshold:9206.920769257813
Amount Re-paid: 828.6228692332031
New loan amount: -61.181130538039724
any advise please?
public class loanPayments {
// place constant declarations (static final) below.
// e.g. 3% interest, 9% repayment, 5% pay rise and 10k theshold
public static void main (String[]arg){
//declare variables
double loan;
double wages;
final int THRESHOLD= 10000;
double netInc=0;
double newBal=0;
double totalLoan;
double deduction;
//print qoute
System.out.println("Enter total amount of student loan taken");
loan=UserInput.readDouble();
//print qoute
System.out.println("Enter anticipated income");
wages=UserInput.readDouble();
while (newBal >= 0) {
newBal=loan*0.03;
netInc=wages-THRESHOLD;
deduction=netInc*0.09;
totalLoan=loan+newBal;
newBal=totalLoan-deduction;
System.out.println("Old loan: "+loan);
System.out.println("Loan plus 3% interest: "+totalLoan);
System.out.println("wages: "+wages);
System.out.println("Wages minus threshold:" +netInc);
System.out.println("Amount Re-paid: "+ deduction);
System.out.println("New loan amount: "+newBal);
loan=newBal+0;
wages=wages +(wages*0.05);
}
System.out.println("end ");
}//end main
}//end class
- 01-16-2009, 04:58 PM #2
Senior Member
- Join Date
- Jan 2009
- Posts
- 119
- Rep Power
- 0
Hello,
I think these would help
java.sun.com/j2se/1.5.0/docs/api/java/math/MathContext . html
java.sun.com / j2se / 1.5.0 / docs / api /
- 01-16-2009, 04:59 PM #3
Senior Member
- Join Date
- Jan 2009
- Posts
- 119
- Rep Power
- 0
I think you can use the MathContext to set the precision. then use the round method contained in the BigDecimal class
java.math.BigDecimal
AND
java.math.MathContext
- 01-16-2009, 05:34 PM #4
Java Code:System.out.printf("Old loan: %.2f%n", loan); System.out.printf("Loan plus 3%% interest: %.2f%n", totalLoan); System.out.printf("wages: %.2f%n", wages); System.out.printf("Wages minus threshold: %.2f%n", netInc); System.out.printf("Amount Re-paid: %.2f%n", deduction); System.out.printf("New loan amount: %.2f%n", newBal);
- 01-16-2009, 06:17 PM #5
Similar Threads
-
round to two decimal places
By javaMike in forum New To JavaReplies: 3Last Post: 12-24-2011, 02:01 AM -
Truncating decimal numbers in the output
By gbade in forum New To JavaReplies: 2Last Post: 11-21-2008, 06:25 PM -
numbers with two decimal places
By little_polarbear in forum New To JavaReplies: 8Last Post: 08-27-2008, 11:04 PM -
How to round a double?
By Valeriano in forum New To JavaReplies: 1Last Post: 05-31-2007, 03:50 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks