DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2004
    Posts
    1

    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.io.InputStreamReader;
    import java.io.BufferedReader;
    import java.util.StringTokenizer;
    import java.io.IOException;

    public class Currency {

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

    final BufferedReader tKeyboard =
    new BufferedReader(new InputStreamReader(System.in));

    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.parseDouble(tTotalAmountString);

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

    }
    }

    Thanks

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

    Code:
    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

Bookmarks

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
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links