# Thread: round down to 2 sig. numbers after the decimal point

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

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");
//print qoute
System.out.println("Enter anticipated income");

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

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 /

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

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);```

5. The BigDecimal class is designed for this sort of thing. float and double are not precise, whereas BigDecimal provides exact results, including control over rounding. I'd use it for calculations, as well as for rounding off the results. See the API.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•