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 6 of 6

Thread: Formatting my output

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Formatting my output

    How do I change my numbers at the end so that instead of showing 0 it shows 0.00 or instead of showing 5.3 it shows 5.30?
    I want only the winnings to be showing in this format.

     
    import java.util.Scanner;
     
    public class part1 {
     
    public static void main(String[] args) 
    	{
    		Scanner keyboard = new Scanner(System.in);
    		double stake;
    		String horsesName;
    		double outrightWinOrEachWay;
    		double outrightWin;
    		double eachWay;
    		double OddsX;
    		double OddsY;
    		double totalOdds;
    		double eachWayOdds;
    		double runners;
    		double finishingPosition;
    		double winnings;
    		double win;
    		double place;
     
     
    				System.out.println("Please enter your stake:");
    				while (! keyboard.hasNextDouble() ) 
    				{  
    				 keyboard.nextLine(); 
    				 System.out.print("Please try again "); 
    				} 
    				stake = keyboard.nextFloat(); 
     
     
    				horsesName = keyboard.nextLine(); 
     
    				while ( horsesName.isEmpty() ) 
     
    				{ 
    				 System.out.print("What is your horses name?"); 
    				 horsesName = keyboard.nextLine(); 
    				} 
     
    				horsesName = horsesName.toUpperCase();
     
    				System.out.println("Enter the betting odds (x/y)");
    				System.out.println("X=");
     
    				while (! keyboard.hasNextDouble() ) 
    				{  
    				 keyboard.nextLine(); 
    				 System.out.print("Please try again "); 
    				} 
    				OddsX = keyboard.nextFloat(); 
     
    				System.out.println("Y=");
    				while (! keyboard.hasNextDouble() ) 
    				{  
    				 keyboard.nextLine(); 
    				 System.out.print("Please try again "); 
    				} 
    				OddsY = keyboard.nextFloat(); 
     
    				totalOdds = (OddsX/OddsY);
     
    				System.out.println("How many runners are in the race?");
    				while (! keyboard.hasNextDouble() ) 
    				{  
    				 keyboard.nextLine(); 
    				 System.out.print("Please try again "); 
    				} 
    				runners = keyboard.nextFloat(); 
     
    				System.out.println("What is your finishing position?");
    				while (! keyboard.hasNextDouble() ) 
    				{  
    				 keyboard.nextLine(); 
    				 System.out.print("Please try again "); 
    				} 
    				finishingPosition = keyboard.nextFloat(); 
     
    				System.out.println("Is your bet a:");
    				System.out.println("(1) Outright Win");
    				System.out.println("(2) Eachway");
    				while (! keyboard.hasNextDouble() ) 
    				{  
    				 keyboard.nextLine(); 
    				 System.out.print("Please try again "); 
    				} 
    				outrightWinOrEachWay = keyboard.nextFloat(); 
     
    				if (outrightWinOrEachWay == 1)
    				{
    					if (finishingPosition == 1)
    					{
    						winnings = stake + (totalOdds * stake);
    						System.out.println(horsesName + " finished 1st out of " + runners + " runners");
    						System.out.println("You have won \u20ac" + winnings);	
    					}
     
    					if (finishingPosition > 1)
    						{
    							System.out.println("Sorry you didn't win");
    						}
    				}
     
    				if (outrightWinOrEachWay == 2)
    				{
    					if (runners < 7)
    					{
     
    						eachWayOdds = (totalOdds/2);
     
    						if (finishingPosition == 1)
    						{
    							win = stake + (totalOdds * stake);
    							place = stake + (eachWayOdds * stake);
    							winnings = win + place;
    							System.out.println(horsesName + " finished 1st out of " + runners + " runners");
    							System.out.println("You have won \u20ac" + winnings);
    						}
     
    						if (finishingPosition ==2)
    						{
    							winnings = stake + (eachWayOdds * stake);
    							System.out.println(horsesName + " finished 2nd out of " + runners + " runners");
    							System.out.println("You have won \u20ac" + winnings);
    						}
     
    						if (finishingPosition> 2){
    							System.out.println("Sorry you didn't win");
    						}
    					}
    						if (runners >= 8 && runners <=20)
    						{
    						eachWayOdds = (totalOdds/3);
     
    						if (finishingPosition == 1)
    						{
    							win = stake + (totalOdds * stake);
    							place = stake + (eachWayOdds * stake);
    							winnings = win + place;
    							System.out.println(horsesName + " finished 1st out of " + runners + " runners");
    							System.out.println("You have won \u20ac" + winnings);
    						}
     
    						if (finishingPosition ==2)
    						{
    							winnings = stake + (eachWayOdds * stake);
    							System.out.println(horsesName + " finished 2nd out of " + runners + " runners");
    							System.out.println("You have won \u20ac" + winnings);
    						}
     
    						if (finishingPosition ==3)
    						{
    							winnings = stake + (eachWayOdds * stake);
    							System.out.println(horsesName + " finished 3rd out of " + runners + " runners");
    							System.out.println("You have won \u20ac" + winnings);
    						}
     
    						if (finishingPosition >3)
    						{
    							System.out.println("Sorry you didn't win");
    						}
    					}
    					if (runners >20)
    					{
    						eachWayOdds = (totalOdds/4);
     
    						if (finishingPosition == 1)
    						{
    							win = stake + (totalOdds * stake);
    							place = stake + (eachWayOdds * stake);
    							winnings = win + place;
    							System.out.println(horsesName + " finished 1st out of " + runners + " runners");
    							System.out.println("You have won \u20ac" + winnings);
    						}
     
    						if (finishingPosition ==2)
    						{
    							winnings = stake + (eachWayOdds * stake);
    							System.out.println(horsesName + " finished 2nd out of " + runners + " runners");
    							System.out.println("You have won \u20ac" + winnings);
    						}
     
    						if (finishingPosition ==3)
    						{
    							winnings = stake + (eachWayOdds * stake);
    							System.out.println(horsesName + " finished 3rd out of " + runners + " runners");
    							System.out.println("You have won \u20ac" + winnings);
    						}
     
    						if (finishingPosition ==4)
    						{
    							winnings = stake + (eachWayOdds * stake);
    							System.out.println(horsesName + " finished 4th out of " + runners + " runners");
    							System.out.println("You have won \u20ac" + winnings);
    						}
     
    						if (finishingPosition >4)
    						{
    							System.out.println("Sorry you didn't win");
    						}
    					}
     
    				}
     
    			}
    }


    --- Update ---

    Also how do I fix the place where it shows my java cause I forgot?


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Formatting my output

    Use the NumberFormat or DecimalFormat class.

    The forum formatting tag is "code" and not "java".
    Improving the world one idiot at a time!

  3. The Following User Says Thank You to Junky For This Useful Post:

    chemical_dog (November 7th, 2013)

  4. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Formatting my output

    Quote Originally Posted by Junky View Post
    Use the NumberFormat or DecimalFormat class.

    The forum formatting tag is "code" and not "java".
    How do I is them?

    --- Update ---

    *How do I do them?
    Sorry

  5. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Formatting my output

    You go to the Java API and read about them. You google for examples. Don't just sit there and expect to be spoonfed.
    Improving the world one idiot at a time!

  6. #5
    Junior Member
    Join Date
    Nov 2013
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Formatting my output

    Quote Originally Posted by Junky View Post
    You go to the Java API and read about them. You google for examples. Don't just sit there and expect to be spoonfed.
    I have tried that and it hasn't worked, I'm not just waiting to be spoonfed, I came on a website that was supposed to help.

  7. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Formatting my output

    I did help you. I told you what class you need to use. But as far as I can tell you did nothing with that information and are simply demanding more help.

    You claim "it hasn't worked". That provides zero information. Did you code get deleted? Did your cat explode? Did all the women in the world grow beards? WHAT? Post the code that you tried and fully explain what the problem is. If you get errors then copy and paste the full and exact error messages.
    Improving the world one idiot at a time!

Similar Threads

  1. Formatting 2d Array Output
    By dspear in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: December 14th, 2011, 06:45 PM
  2. Formatting Output
    By mael331 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: September 14th, 2011, 06:41 PM
  3. Question about formatting output
    By mwr76 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 10th, 2011, 11:57 AM
  4. Formatting output of a Double
    By CarlMartin10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 11th, 2010, 04:20 PM
  5. Replies: 3
    Last Post: August 19th, 2009, 11:30 AM

Tags for this Thread