Это относится к моему предыдущему вопросу, который можно найти по адресу:
Результат математического уравнения теряет десятичные знаки при отображении
В моем задании нам нужно вычислить периметр равнобедренной трапеции. Периметр должен быть отформатирован до 4 знаков после запятой. Если в результате после запятой все нули, то нули не отображать. (Пример: результаты равны 12.000000, будет отображаться 12.) Кроме того, если результат больше 1000 до десятичной точки, запятая должна отображаться. (Пример: результаты 1234,56781, то, что будет отображаться, будет 1,234,5678). Мы обязаны использовать класс десятичного формата. Вот мой код:
//Kyle Collins
/*This program calculates the area and perimeter of an isosceles trapezoid, as well
as the diagonal of the isosceles trapezoid.
*/
import java.util.Scanner;
import java.lang.Math;
import java.text.*;
public class CSCD210Lab2
{
public static void main (String [] args)
{
Scanner mathInput = new Scanner(System.in);
//declare variables
double topLength, bottomLength, height,perimPt1,perimPt2;
//Get user input
System.out.print("Please Enter Length of the Top of Isosceles Trapezoid: ") ;
topLength = mathInput.nextDouble() ;
mathInput.nextLine() ;
System.out.print("Please Enter Length of the Bottom of Isosceles Trapezoid: ") ;
bottomLength = mathInput.nextDouble() ;
mathInput.nextLine() ;
System.out.print("Please Enter Height of Isosceles Trapezoid: ") ;
height = mathInput.nextDouble() ;
mathInput.nextLine() ;
perimPt1 = ((bottomLength - topLength)/2);
perimPt2 =(Math.sqrt(Math.pow(perimPt1,2) + Math.pow(height,2)));
double trapArea = ((topLength + bottomLength)/2*(height));
double trapDiag = (Math.sqrt(topLength*bottomLength + Math.pow(height,2)));
double trapPerim = 2*(perimPt2) + (topLength + bottomLength);
//Print the results
System.out.println();
System.out.println("The Area of the Isosceles Trapezoid is: "+trapArea);
System.out.printf("The Diagonal of the isosceles trapezoid is: %-10.3f%n",trapDiag);
System.out.printf("The Perimeter of the Isosceles Trapezoid is: "+trapPerim );
}
}
Как мне отформатировать распечатку для периметра, чтобы он использовал класс десятичного формата и удовлетворял требованиям?