How to format the result
This is assignment I was given:
Here is my code for the assignment:
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";
System.out.println("Welcome to the Temperature Converter");
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.print("Continue? (y/n): ");
choice = sc.next();
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?
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.
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:
Best of luck, and again, welcome!
// your code goes here
// notice how the top and bottom tags are different
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.