Package com. tq365.util;
Import java. Text. numberformat;
Import java. util. locale;
Public class formattest {
Public static void main (string [] ARGs ){
// No formatted output
Double D = 10000.0/3.0;
System. Out. println ("No formatted output:" + D );
Numberformat = numberformat. getnumberinstance ();
// Retain four decimal places
// Numberformat. setmaximumfractiondigits (4 );
// The first six digits of the decimal point
// Numberformat. setminimumintegerdigits (6 );
// System. Out. println (numberformat. Format (d ));
// The local default format is 3,333.333.
System. Out. println (numberformat. Format (d ));
// Use the local default format to output the currency value
Numberformat currentformat = numberformat. getcurrencyinstance ();
// ¥3,333.33
System. Out. println ("Local default format output currency value:" + currentformat. Format (d ));
// Use the local default format to output the percentage
Numberformat percentformat = numberformat. getpercentinstance ();
System. Out. println ("Local default format output percentage:" + percentformat. Format (d ));
// Use the U.S. currency output form
Numberformat currentformatus = numberformat. getcurrencyinstance (locale. US );
// $3,333.33
System. Out. println ("U.S. currency output form:" + currentformatus. Format (d ));
}
}
Decimalformat
- Import java. Text. decimalformat;
- Public class testnumberformat {
- Public static void main (string [] ARGs ){
- Double Pi = 3.1415927; // circumference Rate
- // Returns an integer.
- System. Out. println (New decimalformat ("0"). Format (PI); // 3
- // Take one integer and two decimal places
- System. Out. println (New decimalformat ("0.00"). Format (PI); // 3.14
- // Take two integers and three decimal places. The less integer is filled with 0.
- System. Out. println (New decimalformat ("00.000"). Format (PI); // 03.142
- // Obtain all integer parts
- System. Out. println (New decimalformat ("#"). Format (PI); // 3
- // Count by percentage and take two decimal places
- System. Out. println (New decimalformat ("#. ### %"). Format (PI); // 314.16%
- Long c = 299792458; // light speed
- // Display as a scientific notation and take five decimal places
- System. Out. println (New decimalformat ("#. ##### E0"). Format (c); // 2.99792e8
- // Display the scientific notation of two integers 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 the format into the text
- System. Out. println (New decimalformat ("the speed of light is every second, ### meters. "). Format (c ));
- }
- }