How to use DecimalFormat

Posted on 2004-09-12
Last Modified: 2010-08-05
I'm a new programming student...this is an assignment so I really need guidance or suggestions on how to make it work correctly.

I have the below code that provides conversion for a selected currency. I need to format the results with two decimal places. I think the DecimalFormat class is need somehow but I am not sure exactly where to insert the class. I tried inserting within the switch statements but keep getting an unreachable statement error (really not sure what that means).

import*;                  // required for BufferedReader

//gives my Java program the name Currency_wj
class Currency_wj {
      //the main part of the program begins here      
      public static void main(String[] arguments) throws Exception {
      //"throws Exception",
      //throws Exception needed when reading
      //from keyboard. Says you will take
      //responsibility for errors.

      //reads selection number from menu entered by user
      BufferedReader      br = new BufferedReader(
      new InputStreamReader(;
      //reads amount entered by user
      BufferedReader money = new BufferedReader(
      new InputStreamReader(;
      double ArgentinaCurrency = 2.95;  //declare and initialize  
      double CanadaCurrency = 1.32;       //declare and initialize
      double calculate = 0.00;            
      String cname1 = "Argentina Peso";  //declare and initialize string
      String cname2 = "\nCanada Dollar"; //declare and initialize string
      String cnames = cname1.concat(cname2); //concatenate two Strings
      String intro = "Welcome to the "; //declare and initialize String
      String name = "Currency Exchange Program!"; //declare and initialize String
      String title = intro.concat(name); //concatenate two Strings
      double dollar; //delcare and initialize
      //display combination of concatenated string             
      System.out.println(title + "\n\nThe currency exchange program shows\n"+
      "the exchange rate of $1 American dollar\n" +
      "for the following currencies:\n\n" + cnames);      
      // prints a blank line
      //display current currency rates
      System.out.println(cname1 + " = " + ArgentinaCurrency + cname2 + " = " +  CanadaCurrency);
      //prints a blank line
      char keep_going = ' ';          // initialize, set initial value
      String inString;            //declare
      char MenuChoice;            //delcare
      dollar = 0.00;            //declare and set initial value
      double amount = 0.0;      //delcare and set initial value
      String data = "oo";            //

      while (keep_going != 'q')       // will continue loop until condition met
            //displays to screen statement in quotes
            System.out.println("\nSelect a currency to convert:\n");
            System.out.println("      1. Argentina Pesos");
            System.out.println("      2. Canada Dollars");
            System.out.println("      3. Exit.");    

            // Send all output to the monitor and make the
            // cursor visible before the user begins typing.
            // "readline()" returns a String which is
            // assigned to a String variable, inString
            inString = br.readLine();
            MenuChoice = inString.charAt(0);
            // get first char of String
            //switch case statement, displays data to screen depending on user selection
                  case '1':           
                  //displays data in quotes to screen      
                  System.out.println("You want to convert Argentina Pesos");
                  //displays data in quotes to screen
                  System.out.print("Enter dollar amount to convert: ");
                  //displays data and displays cursor
                  //stores amount entered by user
                  data = money.readLine();
                  //converts string to double
                  amount = Double.parseDouble(data);
                  //displays amount entered, currency name, calculates conversion and  displays results
      System.out.println(amount + " Argentina Pesos equal " + amount*ArgentinaCurrency + " American Dollars");
                            case '2':
                  //displays data in quotes to screen
                  System.out.println("You want to convert Canada Dollars");
                  //displays data in quotes to screen
                  System.out.print("Enter dollar amount to convert: ");
                  //displays data and displays cursor
                  //stores amount entered by user
                  data = money.readLine();
                  //converts string to double
                  amount = Double.parseDouble(data);
                  //displays amount entered, currency name, calculates conversion and displays results
                  System.out.println(amount + " Canada Dollars equal " + amount*CanadaCurrency + " American Dollars");
                  case '3':
                  //condition met -- program exits
                  keep_going = 'q';
                  //displays data to screen when invalid entry entered
                  System.out.println("You entered an invalid number, please enter 1-3");                  break;
            } // end switch
      }      // end while

      //displays data in quotes to screen      
      System.out.print("\nThank you for using the Currency Exchange Program\n");
            }//end main
      }//end class

Any suggestions would be greatly appreciated.
Question by:contesa
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +2
LVL 92

Assisted Solution

objects earned 150 total points
ID: 12040642
create a format like so:

DecimalFormat df = new DecimalFormat("0.00");

and then use it:

System.out.println(amount + " Canada Dollars equal " + df.format(amount*CanadaCurrency) + " American Dollars");

Accepted Solution

Emilda earned 150 total points
ID: 12040816
Include the below statements in your program just before printing the converted values. You can restrict to two decimal places using the format given by the variable myFormat.

Hope this helps.

amount = amount*ArgentinaCurrency;
String myFormat = "#.00";
DecimalFormat df = new DecimalFormat(myFormat);            
String result = df.format(amount);
//displays amount entered, currency name, calculates conversion and  displays results
System.out.println(amount + " Argentina Pesos equal " + result + " American Dollars");
LVL 16

Assisted Solution

suprapto45 earned 200 total points
ID: 12041138
Hi contesa,

Emilda is right,

add these codes...

String myFormat = "#.00";
DecimalFormat df = new DecimalFormat(myFormat);
               //displays amount entered, currency name, calculates conversion and  displays results
     System.out.println(amount + " Argentina Pesos equal " + df.format(amount*ArgentinaCurrency) + " American Dollars");

and not forgetting to

import java.text.*;

Thanks and I hope that helps. Remember object and Emilda who helped you.
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 12041222

Author Comment

ID: 12050788
You guys are awesome...thank you.


LVL 92

Expert Comment

ID: 12050822
no worries :)

Expert Comment

ID: 12050890

Thank you.
LVL 16

Expert Comment

ID: 12054997

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
by zero exception 10 69
maven module vs maven project 3 73
Android development question 2 78
Crystal Reports Licensing Questions 4 60
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question