In: Computer Science
System.out.println("Enter a temperature in Fahrenheit:
");
double fahrenheit = input.nextDouble();
double celsius = (5.0 / 9) * (fahrenheit - 32);
System.out.println("Fahrenheit " + fahrenheit + " is "
+ celsius + " in Celsius");
the above code i would like to print to 2 decimal places for celsius please
this is in JAVA
please find your solution below and if any doubt comment and do upvote.
CODE:
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("Enter a temperature in Fahrenheit: ");
double fahrenheit = input.nextDouble();
double celsius = (5.0 / 9) * (fahrenheit - 32);
//System.out.println("Fahrenheit " + fahrenheit + " is " + celsius + " in Celsius");
//the is used to round off to twp decimal place System.out.printf("%.2f",value)
//the .2 tell to print variable to 2 decimal place
//for no precision remove the .2 or to increase or decrease precision add suitale number after .(dot) i.e .5,.1 etc
System.out.printf("Fahrenheit %.2f is %.2f in Celsius",fahrenheit,celsius);
}
}
OUTPUT: