Results 1 to 5 of 5
  1. #1
    soc86 is offline Member
    Join Date
    Oct 2008
    Posts
    39
    Rep Power
    0

    Default 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

  2. #2
    MuslimCoder is offline Senior Member
    Join Date
    Jan 2009
    Posts
    119
    Rep Power
    0

    Default

    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. #3
    MuslimCoder is offline Senior Member
    Join Date
    Jan 2009
    Posts
    119
    Rep Power
    0

    Default

    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. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    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. #5
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    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.

Similar Threads

  1. round to two decimal places
    By javaMike in forum New To Java
    Replies: 3
    Last Post: 12-24-2011, 02:01 AM
  2. Truncating decimal numbers in the output
    By gbade in forum New To Java
    Replies: 2
    Last Post: 11-21-2008, 06:25 PM
  3. numbers with two decimal places
    By little_polarbear in forum New To Java
    Replies: 8
    Last Post: 08-27-2008, 11:04 PM
  4. How to round a double?
    By Valeriano in forum New To Java
    Replies: 1
    Last Post: 05-31-2007, 03:50 PM

Posting Permissions

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