How to format Number to certain number of Decimal Places plus comma separators?

早过忘川 提交于 2019-12-11 16:19:59

问题


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?


回答1:


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




回答2:


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


来源:https://stackoverflow.com/questions/26128526/how-to-format-number-to-certain-number-of-decimal-places-plus-comma-separators

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!