How to format Number to certain number of Decimal

2019-09-09 21:42发布

This relates to my previous question, which can be found at:

Math equation result loses decimals when displayed

In my assignment, we have to calculate the perimeter of an isosceles trapezoid. The perimeter the needs to be formatted to 4 decimal places. If the result after the decimal place is all zeros, then don’t display the zeros. (Example: the results are 12.000000 what will be displayed is 12.) Also if the result is greater than 1000 before the decimal, then the comma must be displayed. (Example: the results are 1234.56781 what will be display is 1,234.5678). We are required to use the decimal format class. Here is my code:

//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 );
   }
}

How would I format the print out for the perimeter so that it uses the decimal format class and satisfies the requirements?

2条回答
淡お忘
2楼-- · 2019-09-09 22:17

Use DecimalFormat to format numbers:

DecimalFormat df = new DecimalFormat("#,###.####", new DecimalFormatSymbols(Locale.US));
System.out.println(df.format((double)12.000000));
System.out.println(df.format((double)1234.56781));
System.out.println(df.format((double)123456789.012));

This pattern here is only necessary to cut after the 4th decimal place, like your second example suggested. If you don't want that new DecimalFormat("", new DecimalFormatSymbols(Locale.US)) would also work.

(It is necessary to set the format symbols or the symbols of the current locale will be used. These symbols may differ from the desight ones)

The output will be

12
1,234.5678
123,456,789.012
查看更多
做个烂人
3楼-- · 2019-09-09 22:29

Use and String.format()

String.format("%.2f", (double)value);

There are various ways to define this please see what you need. You can also use Decimal Format

查看更多
登录 后发表回答