Results 1 to 3 of 3

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

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2009
    Posts
    5

    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;
    double firstBal;
    int year=1;


    //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();

    firstBal = newBal-0;

    while (newBal >= 0) {

    newBal=loan*0.03;

    netInc=wages-THRESHOLD;
    deduction=netInc*0.09;

    totalLoan=loan+newBal;

    newBal=totalLoan-deduction;



    System.out.println("Year: "+year+" 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);
    year=year+1;
    }

    year=year-1;

    System.out.println("It took you "+year+" years to repay the loan");
    System.out.println("The original loan was "+firstBal+" but you had to pay back £4,801.08");






    }//end main

    }//end class

  2. #2
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

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

    Have a look at the DecimalFormat class: http://java.sun.com/j2se/1.4.2/docs/...malFormat.html


    Has someone helped you? Then you can Rate their helpful post.

  3. #3
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

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

    Notbad and soc86 are in the race for who gets the answer first Lole

    I've added a suggestion to use String.format

    Sample usage on Notbad's thread.

Posting Permissions

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



Click Here to Expand Forum to Full Width