Need help trying to get output correct to two decimal places.

DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Need help trying to get output correct to two decimal places.

  1. #1
    Join Date
    Oct 2004

    Need help trying to get output correct to two decimal places.

    Hi, I have a problem, I'm trying to write a currency converter and have a problem, I want it to be correct to two decimal places, while I have got it to work, when I have two zero's after the point it only displays one, ie: 250 inputted by the user would be outputed like this: 250.0 instead of the 250.00 I want, my code is below, any help would be much appreciated.

    import java.util.StringTokenizer;

    public class Currency {

    public static void main(final String[] pArgs) throws IOException

    final BufferedReader tKeyboard =
    new BufferedReader(new InputStreamReader(;

    System.out.println("Type currency converting from, to, conversion rate and the amount.\n");
    System.out.println("Seperate them with a space: \n");
    String tLine = tKeyboard.readLine();
    StringTokenizer tTokens = new StringTokenizer(tLine);
    String tFromString = tTokens.nextToken();
    String tFrom = tFromString;
    String tToString = tTokens.nextToken();
    String tTo = tToString;
    String tExchangeString = tTokens.nextToken();
    double tExchange = Double.parseDouble(tExchangeString);
    String tTotalAmountString = tTokens.nextToken();
    double tTotalAmount =

    double tTotal1;
    double tTotal2;
    int tTotal3;
    int tTotal4;
    double tAmountc;
    double tAmounte;
    //Calculation for Exchange Rate.\\
    tTotal1 = 1/tExchange;

    tAmounte = 100.0;

    tTotal4 = (int) (tTotal1 * tAmounte + 0.5);

    double tAmounte2 = tTotal4/tAmounte;
    //Calculation for Output Amount.\\
    tTotal2 = tExchange * tTotalAmount;

    tAmountc = 100.0;

    tTotal3 = (int) (tTotal2 * tAmountc + 0.5);

    double tAmountc2 = tTotal3/tAmountc;

    System.out.println("\t " + tFrom + "\t " + tTo);
    System.out.println("\t 1.00" + "\t " + tExchange);
    System.out.println("\t " + tAmounte2 + "\t 1.00");
    System.out.println("\t " + tTotalAmount + "\t " + tAmountc2);



  2. #2
    Join Date
    Sep 2004
    here is a small example of how to use the decimal format class:

    import java.text.DecimalFormat;
    import java.text.DecimalFormatSymbols;
        float X,Y = //some numbers;
        String format1 = "###0.00" ;  
        static DecimalFormat fm1 = new DecimalFormat( format1,
                                       new DecimalFormatSymbols(Locale.US));
        System.out.println("X=" + fm1.format(x) + ", Y=" + fm1.format(y));
    this will always printout to 2 decimal places
    A kram a day keeps the doctor......guessing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center