DecimalFormat usage

zhaozj2021-02-16  54

Import java.text. *; import java.util. *;

Public class decimalformatsample {public static void main (String args []) {decimalformat myformat1 = new decimalformat ("###, ###. 0000"); // Using the system default format system.out.println (MyFormat1.Format (11111123456.12));

Locale.SETDEFAULT (Locale.us); decimalformat myformat2 = new decimalformat ("###, ###. 0000"); // uses the US format System.Out.println (MyFormat2.Format (11111123456.12);

// ----------------------------------------------------- ------------- //

Decimalformat myformat3 = new decimalformat (); myFormat3.applypattern ("##, ###. 000"); system.out.println (MyFormat3.Format (11112345.12345)); // ------------ ------- Control Index Output ---------------------------------------- --------- //

Decimalformat myformat4 = new decimalformat (); myFormat4.applypattern ("0.000e0000"); System.out.println (MyFormat4.Format); System.Out.println (MyFormat4.Format (12345678.345)); // - --------------------------------------------- ------------ /// * DecimalFormat is a subclass of NumberFormat, which is designated as a specific area. Therefore, you can specify a region using NumberFormat.getInstance, and then force the structure to a DecimalFormat object. This technology mentioned in the document can be applied in most cases, but you need to enclose forced conversion with the try / catch block to prevent switching that does not work properly (probably uses a singular area under very unspeakable). * / DecimalFormat myformat5 = null; try {myformat5 = (DecimalFormat) NumberFormat.getPercentInstance ();} catch (ClassCastException e) {System.err.println (e);} myformat5.applyPattern ( "00.0000%"); System.out .println (MyFormat5.Format (0.34567)); System.out.println (MyFormat5.Format (1.34567));

}

/*---------------------------------operation result------------- ------------------------------ // f: / 2004-04-12> Java DecimalFormatsample111, 111, 123, 456.1200111, 111, 123, 456.120011, 112, 345.1231. 000E00041.235E000734.5670% 134.5670% * /

}

转载请注明原文地址:https://www.9cbs.com/read-23098.html

New Post(0)