Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: New to Java Need 2 decimal places, please :). Here is my code

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default New to Java Need 2 decimal places, please :). Here is my code

    import java.io.*;
     
    public class Balance
     
    {
    	public static void main(String[] args) throws IOException
    	{
     
    		BufferedReader dataIn= new BufferedReader(new InputStreamReader(System.in));
     
    		// declare and construct variables
    		String sBeginningBalance, sTotalDeposits, sTotalChecks, sTotalFees;
    		float fBeginningBalance, fTotalDeposits, fTotalChecks, fTotalFees;
    		float fEndingBalance = 0;
     
     
     
    		//prompts and gets input
    		System.out.println("BALANCING YOUR CHECKBOOK");
    		System.out.print("Enter the balance of your last statement? $");
    			sBeginningBalance = dataIn.readLine();
    			fBeginningBalance = Float.parseFloat(sBeginningBalance);
    		System.out.print("What is the total amount of all deposits? $");
    			sTotalDeposits = dataIn.readLine();
    			fTotalDeposits = Float.parseFloat(sTotalDeposits);
    		System.out.print("What is the total amount of all checks? $");
    			sTotalChecks = dataIn.readLine();
    			fTotalChecks = Float.parseFloat(sTotalChecks);
    		System.out.print("What is the amount of all transaction fees? $");
    			sTotalFees = dataIn.readLine();
    			fTotalFees = Float.parseFloat(sTotalFees);
     
    		//Data conversion
    		    double dBeginningBalance = fBeginningBalance;
    		    double dTotalDeposits = fTotalDeposits;
    		    double dTotalChecks = fTotalChecks;
    		    double dTotalFees = fTotalFees;
     
    		fEndingBalance = (fBeginningBalance + fTotalDeposits - fTotalChecks - fTotalFees);
     
     
    		System.out.print("Your new balance is: " + fEndingBalance + ".");
    	}//end main
    }//end class


  2. #2
    Junior Member Mrc0d3r's Avatar
    Join Date
    Jun 2011
    Location
    TCP/IP Layer 3
    Posts
    25
    My Mood
    Bored
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default Re: New to Java Need 2 decimal places, please :). Here is my code

    //import the DecimalFormat class..
    import java.text.DecimalFormat;

    Add this piece of code before printing out the result :

    DecimalFormat decimalFormat = new DecimalFormat("#.##");
    //change the print statement to the below line..
    System.out.print("Your new balance is: " + Double.valueOf(decimalFormat.format(fEndingBalance)) + ".");

    P.S: Use code tags next time you post. Also, post under right section.
    Last edited by Mrc0d3r; June 27th, 2011 at 11:05 AM.

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default Re: New to Java Need 2 decimal places, please :). Here is my code

    Welcome to the forums Charyl.

    In future, please give us as much information as possible about your issue. Posting code without any background information means you limit the replies you will receive.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

Similar Threads

  1. Java program to format a double value to 2 decimal places
    By JavaPF in forum Java Programming Tutorials
    Replies: 3
    Last Post: December 4th, 2010, 03:08 PM
  2. Java program to format a double value to 2 decimal places
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 3
    Last Post: December 4th, 2010, 03:08 PM
  3. Limiting decimal places in a double
    By darek9576 in forum Object Oriented Programming
    Replies: 2
    Last Post: March 14th, 2010, 11:27 AM
  4. float to 2 decimal places
    By fh84 in forum Java Theory & Questions
    Replies: 3
    Last Post: November 25th, 2009, 10:27 AM
  5. [SOLVED] How to use decimal place when formatting an output?
    By napenthia in forum Java Theory & Questions
    Replies: 2
    Last Post: April 27th, 2009, 03:17 AM