-
One Decimal Place
Hello there... I have this code. I want to round the converted value to one decimal place. How can I do that? Thank you.. :)
Code:
public class darjahCelcius{
public static void main(String[] args) {
System.out.println("Celsius\t\tFahrenheit\t|\tFahrenheit\t\tCelsius");
System.out.println("---------------------------------------------");
double celsius = 40; double fahrenheit = 120;
for (int i = 1; i <= 10; celsius--, fahrenheit -= 10, i++) {
System.out.println(celsius + "\t\t" +
celsiusToFahrenheit(celsius) + "\t\t|\t" + fahrenheit + "\t\t\t" +
fahrenheitToCelsius(fahrenheit));
}
}
public static double celsiusToFahrenheit(double celsius) {
return (9.0 / 5.0) * celsius + 32;
}
public static double fahrenheitToCelsius(double fahrenheit) {
return (5.0 / 9) * (fahrenheit - 32);
}
}
-
Re: One Decimal Place
First of all, your code looks horrible. I'm not saying it's bad (though not good either), only that it looks terrible. It's hard to read and follow your code without getting lost in what you have done. Look at other threads or Java code examples and learn how to indent and structure your code properly.
To answer your question, look into the class DecimalFormat. Read about it in the Java API page: DecimalFormat (Java Platform SE 7 )
Look into how you set up a DecimalFormat and then at the methods to apply it to your numbers. To accomplish what you want, you only need one new code line and two modifications of your code.