System.out.printf ("\nThe equation has two roots: %8.5f and %8.5f ", r1, r2);
i wrote a program that gives me a lot of decimal paces in the answer how can i restricted to 6 decimal places?
the code is the following:
import java.util.Scanner;
public class HW3_1 {
public static void main(String[] args) {
System.out.print("Enter a,b,c: ");
Scanner imput = new Scanner(System.in);
double a = imput.nextDouble();
double b = imput.nextDouble();
double c = imput.nextDouble();
double d = Math.pow( b , 2 );
double discriminate = d - (4 * a * c);
double r1 = ((-b + Math.pow(discriminate,.5)) / (2 * a));
double r2 = ((-b - Math.pow(discriminate,.5)) / (2 * a));
if (discriminate > 0){
System.out.println("the equation has two roots: " + r1 + " and " + r2);
}
else if (discriminate == 0){
System.out.println("the equation has one root: " + r1);
}
else if (discriminate < 0){
System.out.println("the equation has no real roots");
}
}
}
When i use: 1,3,1 as the imput i want it to display only the first 5 decimal places. PLEASE HELP!!!