- // take an integer
- System.out.println(new DecimalFormat("0").format(pi));// 3
- // take one integer and two decimals
- System.out.println(new DecimalFormat("0.00").format(pi));// 3.14
- // Take two integers and three decimals, and fill in the missing part of the integer with 0.
- System.out.println(new DecimalFormat("00.000").format(pi));// 03.142
- // get all integer parts
- System.out.println(new DecimalFormat("#").format(pi));// 3
- // Count as a percentage with two decimal places
- System.out.println(new DecimalFormat("#.##%").format(pi));// 314.16%
- long c = 299792458;
- // Display in scientific notation with five decimal places
- System.out.println(new DecimalFormat("#.#####E0").format(c));// 2.99792E8
- // Display scientific notation as a two-digit integer with four decimal places
- System.out.println(new DecimalFormat("00.####E0").format(c));// 29.9792E7
- // Each three digits are separated by commas.
- System.out.println(new DecimalFormat(",###").format(c));// 299,792,458
- // embed formatting into text
- System.out.println( new DecimalFormat( "The speed of light is per second, ###meters." ).format(c));
Java Advanced Data Formatting
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=325969060&siteId=291194637
Recommended
Ranking