Format numbers with DecimalFormat
Introduction Output of floating point numbers in Java indicates that the number of floats in Java includes basic float, double, and object packaging type FLOAT and DOUBLE, for these floats, whether it is explicitly or implicitly called toString () Getting its representation string, the output format is performed in accordance with the following rules If the absolute value is greater than 0.001, less than 10000000, then it is expressed in a conventional decimal form. If the scientific count method is represented outside of the above range. That is, similar to 1.234E8 form.
You can use Java.Text.DecimalFormat and its parent NumberFormat to format the number of DecimalFormat.
Pattern0 - If there is no number in the corresponding location, use zero instead of # - If there is no number on the corresponding position, keep it in the original (no need to make); if it is the most, it is 0, it remains empty. Positive and negative number template segmentation (;)
Number Format Pattern Syntax You can design your own format patterns for numbers by following the rules specified by the following BNF diagram: pattern: = subpattern {; subpattern} subpattern: = {prefix} integer {.fraction} {suffix} prefix: = ' //u0000'..'//ufffd '- SpecialcharactersSuffix: =' //u0000'..'//ufffd '- Specialcharactersinteger: =' # '*' 0 '*' 0'fraction: = '0' * ' # '*
Demovalue123456.789
Pattern, ###. ###
Output123, 456.789
ExplanationThe Pound Sign (#) Denotes a Digit, The Comma (comma) IS a placeholder for the grouping separator, and the period (Junction) IS a placeholder for the decimal separator. Well number (#) represents a number, comma is used The placeholder of the packet separator is point the placeholder of the decimal point. If the right side of the decimal point, there is three values, but the model is only two. The Format method is processed by four rounds.
Value123.78
Pattern000000.000
Output000123.780
EXPLANATIONTHE PATTERN Specifies Leading and trailing Zeros, Because The 0 Character IS Used INSTEAD OF THE POUND SIGN (#).
Application Example 1: / * * Copyright (c) 1995-1998 Sun Microsystems, Inc. All Rights Reserved * / import java.util *; import java.text *; public class DecimalFormatDemo {static public void customFormat (String pattern.. , double value) {DecimalFormat myFormatter = new DecimalFormat (pattern); String output = myFormatter.format (value); System.out.println (value "" pattern "" output);} static public void localizedFormat (String pattern, double value, Locale loc) {NumberFormat nf = NumberFormat.getNumberInstance (loc); DecimalFormat df = (DecimalFormat) nf; df.applyPattern (pattern); String output = df.format (value); System.out.println ( Pattern " Output " Loc.tostring ());} static public void main (string [] args) {CustomFormat ("###, ###. ###", 123456.789); Customformat "###. ##", 123456.789); CustomFormat ("000000.000", 123.78); CustomFormat ("$ ###, ###. ###", 12345.67); CustomFormat ("/ u00A5 ###, ####### ", 12345.67); Locale Currentlocale = New Locale (" En "," US "); DecimalFormatSymbols unusualSymbols = new DecimalFormatSymbols (currentLocale); unusualSymbols.setDecimalSeparator ( '|'); unusualSymbols.setGroupingSeparator ( '^'); String strange = "#, ## 0 ###."; DecimalFormat weirdFormatter = new DecimalFormat (strange, unusualSymbols); weirdFormatter.setGroupingSize (4); String bizarre = weirdFormatter.format (12345.678); System.out.println (bizarre); Locale [] locales = {new Locale ( "en", "US"), new Locale ( "de", "de"), new local, "fr"
)}; For (int i = 0; i / / ----------------------------------- / / Can modify the format template / / reserved in the runtime, if the decimal point is not enough 2 decimals will make up zero A.ApplyPattern ("00.00"); s = a.format (333.3); system . // ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ ------------ / / -------------------------------------------------------------------------------------------- // Add a thousand semicaral a.applypattern (". ## / u2030"); s = a.Format (0.78934); system.err.println (s); // After adding thousands, decimal will enter three And plus thousands // ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------- / / -------------------------------------------------------------------------------------------- // Add a percentage A.ApplyPattern ("#. ##%"); s = a.Format (0.78645); system.err.println (s); // ----------- ------------------------------------- / / -------------------------------------------------------------------------------------------- // Before adding, the post-decoration string, remember to enclose a.applypattern ("'this is my money $', ####### = a.format ( 2533333443.3333); System.err.Println (s); // ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ ------------ // ------------------------------------ ------------ // Add currency representation symbol (different countries, add symbols different A.ApplyPattern ("/ u00A4"); s = a.format (34); System.err .println (s); // ------------------------------------------ -------- / / ----------------------------------- / / Define the positive and negative number template, remember to separate A.ApplyPattern ("0.0; '@' - #. 0"); s = a.mmat (33); system.rr.println (s); s = a.Format (-33); system.err.println (s); // ------------------------------ ----------------- // Comprehensive application, different pre-burning string pattern = "'My Monny' ###, ###. ## 'Rmb'; 'UR Money' ###, ###. ## 'us '"; A.ApplyPattern (Pattern); System.out.println (A.Format (-1223233.456)); Public static void main (string [] args) {test test1 = new test ();}}