Results 1 to 4 of 4
  1. #1
    galagali is offline Member
    Join Date
    Jul 2010
    Posts
    1
    Rep Power
    0

    Default How to format the result

    Hello,

    This is assignment I was given:
    https://docs.google.com/fileview?id=...Yjc5NGY1&hl=en

    Here is my code for the assignment:

    Java Code:
    import java.util.Scanner;
    
    public class TemperatureConverter
    {
    	public static void main(String[] args)
    	{
    		Scanner sc = new Scanner(System.in);
    		double x = 5.0;
    		double y = 9.0;
    		double Multiplier = x / y;
    		String choice = "y";
    		while (choice.equalsIgnoreCase("y"))
    		{
    			System.out.println("Welcome to the Temperature Converter");
    			System.out.println();
    
    			System.out.print("Enter degrees in Fahrenheit:  ");
    			double Fahrenheit = sc.nextDouble();
    
    			double Celsius = Multiplier * (Fahrenheit - 32);
    			System.out.print("Degrees in Celsius:  " + Celsius);
    			System.out.println();
    			System.out.println();
    
    			System.out.print("Continue? (y/n):  ");
    			choice = sc.next();
    			System.out.println();
    		}
    	}
    }

    In the assignment, the Celsius output value stays within two decimal places. In my program, if I input 77.5, I don't get 25.28. I get 25.277777778. How do I limit the number of decimal places it goes to? Also, if I manage to limit it to two decimal places, then I don't want output values like 100.00 instead of plain 100. Is there a way to allow two decimals only when there is no whole number?

    Thanks,

    Galagali
    Last edited by Fubarable; 07-11-2010 at 12:12 AM. Reason: Moderator Edit: Code tags added

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,564
    Rep Power
    23

    Default

    Look at the DecimalFormat class. You'll probably find samples of how to use it if you Search this forum.

    Also the printf() method has formatting capabilities.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Hello, and welcome to the forum. I hope you don't mind that I edited your code and added code tags which should help make your posted code retain its formatting and be more readable.

    To do this yourself, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:

    Java Code:
    [code]
      // your code goes here
      // notice how the top and bottom tags are different
    [/code]
    Best of luck, and again, welcome!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Also, I see that you cross-posted this at the Sun New to Java forum: New To Java - How to format the result

    The problem is that you're asking unpaid volunteers to help you here, not paid consultants, and most of us mind if we take time away from work/lives/play to try to help someone only to find that we've provided a solution that was already provided in a cross-post hours ago. The proper thing to do is that if you feel you must cross-post, at least provide links in each cross-post to the other, so that folks don't waste time answering something already answered and so that folks can review all discussions that have already occurred.

Similar Threads

  1. Replies: 7
    Last Post: 05-25-2010, 08:50 AM
  2. Replies: 2
    Last Post: 02-12-2010, 03:39 PM
  3. uncorrect result
    By jamborta in forum New To Java
    Replies: 3
    Last Post: 11-11-2009, 01:17 PM
  4. how to convert one format to another format
    By mahipal_reddy621 in forum New To Java
    Replies: 1
    Last Post: 12-02-2008, 10:21 AM
  5. getting a random result
    By gradon in forum New To Java
    Replies: 2
    Last Post: 07-19-2007, 03:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •