Results 1 to 3 of 3

Thread: Decimal Problem Issue

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2009
    Posts
    4

    Decimal Problem Issue

    Hi All hope someone can save my life.

    I have got the calculations working, but not sure how to round up to pence and pound (round up to 2 decimal places), but on my final output its displaying 134.873563763 for example when I need 134.87 for example. I have highlighted the code below which I seem to be having problems with

    public class ProcessCurrency {

    static final int min_comm = 500;

    public static void main(String[] argv) {


    double commRate, foreignAmount, exchangeRate, penceAmount,actPayOut, actPoundPence;
    String currencyToConverFrom;
    double comm = 0;
    double commPoundPence = 0;
    int trans=0;
    double totForeign=0, totSterling=0, totHandouts=0, totComission=0, avgHand=0,forSter=0;

    System.out.print("Currency to convert from: ");
    currencyToConverFrom = UserInput.readString();

    System.out.print("Rate of exchange to Sterling pence: ");
    exchangeRate = UserInput.readDouble();

    System.out.print("commision Rate: ");
    commRate = UserInput.readDouble();

    System.out.print("Amount to exchange: ");
    foreignAmount = UserInput.readDouble();




    while (foreignAmount != 0){
    penceAmount = foreignAmount / exchangeRate;

    System.out.println(foreignAmount + " " + currencyToConverFrom + " is "+ penceAmount + " pence");



    if (penceAmount < min_comm)
    System.out.println("Amount too small");
    else {
    comm = penceAmount * (commRate/100);
    trans++;
    if (comm < min_comm){
    comm = 500;
    }// PAmt<min100);

    System.out.println("Commission is "+(int)(comm/100)+" pounds "+(int)(comm%100)+" pence ");

    actPayOut = penceAmount - comm;

    actPoundPence = actPayOut;

    System.out.println("Actual sterling amount is "+(int)actPoundPence/100+" pounds "+(int)(actPayOut%100)+" pence");

    totForeign+=foreignAmount;
    totSterling+=penceAmount;
    totHandouts+=actPayOut;
    totComission+=comm;
    avgHand = totHandouts/trans;
    forSter = totForeign/totHandouts;

    System.out.println(foreignAmount + " " + currencyToConverFrom +" is "+ penceAmount%100 +"; commission "+comm/100+" GBP; payout" + actPayOut/100 + " GBP");
    }//end else

    System.out.print("Amount to exchange: ");
    foreignAmount = UserInput.readDouble();
    }//end while


    System.out.println("Transactions " + trans);
    System.out.println("Total "+ currencyToConverFrom +" received " + totForeign);
    System.out.println("Total sterling value " + totSterling);
    System.out.println("Total handouts " + totHandouts);
    System.out.println("Total Commission " + totComission);
    System.out.println("Average handout " + avgHand);
    System.out.println(currencyToConverFrom +" per pound " + forSter);


    }

    }

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

    Re: Decimal Problem Issue

    Read about the DecimalFormat Class.

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

    Re: Decimal Problem Issue

    And in java 1.5 you can also use String.format

    here is sample code:

    Code:
    double nd = -123.456789d;
    		double pd = 987.656789d;
    		System.out.println(String.format("Rounded Value is %1$.2f and full Value is %1$f", nd));
    		System.out.println(String.format("Rounded Value is %1$.2f and full Value is %1$f", pd));
    For the flags and converstion types and the format read this up.

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