Java Advanced Data Formatting

  1.         // take an integer  
  2.         System.out.println(new DecimalFormat("0").format(pi));// 3  
  3.         // take one integer and two decimals  
  4.         System.out.println(new DecimalFormat("0.00").format(pi));// 3.14  
  5.         // Take two integers and three decimals, and fill in the missing part of the integer with 0.  
  6.         System.out.println(new DecimalFormat("00.000").format(pi));// 03.142  
  7.         // get all integer parts  
  8.         System.out.println(new DecimalFormat("#").format(pi));// 3  
  9.         // Count as a percentage with two decimal places  
  10.         System.out.println(new DecimalFormat("#.##%").format(pi));// 314.16%  
  11.   
  12.         long c = 299792458;  
  13.         // Display in scientific notation with five decimal places  
  14.         System.out.println(new DecimalFormat("#.#####E0").format(c));// 2.99792E8  
  15.         // Display scientific notation as a two-digit integer with four decimal places  
  16.         System.out.println(new DecimalFormat("00.####E0").format(c));// 29.9792E7  
  17.         // Each three digits are separated by commas.  
  18.         System.out.println(new DecimalFormat(",###").format(c));// 299,792,458  
  19.         // embed formatting into text  
  20.         System.out.println( new  DecimalFormat( "The speed of light is per second, ###meters." ).format(c));  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325969060&siteId=291194637