DecimalFormat is a concrete subclass of NumberFormat, used to format decimal numbers.
DecimalFormat contains a pattern and a set of symbols
Symbol Meaning:
The following characters are used in non-localized mode. The localized pattern uses the corresponding characters obtained from the DecimalFormatSymbols object of this formatter, which have lost their special state. The two exceptions are currency symbols and quotes, which do not localize.
import java.text.DecimalFormat; public class TestDecimalFormat { public static void main(String[] args) { DecimalFormat df = new DecimalFormat(); double data = 1203.405607809; System.out.println("Before formatting: " + data); String pattern = "0.0";//1203.4 df.applyPattern(pattern); System.out.println("Use" + pattern + "After formatting: " + df.format(data)); // You can add any character you want after the pattern, such as the unit pattern = "000000000.000kg";//00001203.406kg df.applyPattern(pattern); System.out.println("Use" + pattern + "After formatting: " + df.format(data)); //# means that if there is a character, it will be displayed. If it does not exist, it will not be displayed. It can only be used at both ends of the pattern = "##000.000kg";//1203.406kg df.applyPattern(pattern); System.out.println("Use" + pattern + "After formatting: " + df.format(data)); //- means that the output is a negative number and must be placed at the front pattern = "-000.000";//-1203.406 df.applyPattern(pattern); System.out.println("Use" + pattern + "After formatting: " + df.format(data)); //, is a group separator: output result 12,03.41 pattern = "-0,00.0#";//-12,03.41 df.applyPattern(pattern); System.out.println("Use" + pattern + "After formatting: " + df.format(data)); //E means that the output is an exponent. The string before "E" is the format of the base number, and the format of the exponent is the format of the exponent. pattern = "0.00E000";//1.20E003 df.applyPattern(pattern); System.out.println("Use" + pattern + "After formatting: " + df.format(data)); //% means multiply by 100 and displayed as a percentage, and should be placed in the last pattern = "0.00%";//120340.56% df.applyPattern(pattern); System.out.println("Use" + pattern + "After formatting: " + df.format(data)); //"/u2030" means multiply by 1000 and displayed as a thousandth, and should be placed in the last pattern = "0.00/u2030";//203405.61‰ df.applyPattern(pattern); System.out.println("Use" + pattern + "mode format: " + df.format(data)); //"/u00A4" currency symbol, be placed at both ends*****1203.41¥ pattern = "0.00/u00A4";//1203.41¥ df.applyPattern(pattern); System.out.println("Use" + pattern + "mode format: " + df.format(data)); //' is used to quote special characters in the prefix or or suffix. To create a single quote itself, use two single quotes in succession: "# o''clock". pattern = "'#'#" ;//#1203 // pattern = "'#'" ;//#1203 df.applyPattern(pattern); System.out.println("Use" + pattern + "After formatting: " + df.format(data)); pattern = "# o''clock" ;//1203 o'clock df.applyPattern(pattern); System.out.println("Use" + pattern + "After formatting: " + df.format(data)); //''Single quotes in the middle or after is displayed at the end, and when placed in the front is displayed at the front // pattern = "# o''clock.000" ;//1203.406 o'clock // pattern = "# .000o''clock";//1203.406 o'clock // pattern = "# .000''";//1203.406 ' // pattern = "# .''000";//1203.406 ' pattern = "''# .000";//'1203.406 df.applyPattern(pattern); System.out.println("Use" + pattern + "After formatting: " + df.format(data)); } } The output result is:
Before formatting: 1203.405607809 After formatting in 0.0 mode: 1203.4 After formatting in 000000000.000kg mode: 00001203.406kg After formatting in ##000.000kg mode: 1203.406 After formatting in -000.000 mode: -1203.406 After formatting in -0,00.0# mode: -12,03.41 After formatting in 0.00E000 mode: 1.20E003 After formatting in 0.00% mode: 120340.56% After formatting in 0.00‰ mode: 1203405.61‰ After formatting in 0.00¤ mode: 1203.41¥ After formatting in '#'# mode: #1203 After formatting in #o''clock mode: 1203 o'clock After formatting in ''# .000 mode: '1203.406
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.