DecimalFormat applyPattern() method in Java

The applyPattern() Method of DecimalFormat class in Java is used to apply a given string pattern to this Decimal format. This pattern can be set by the user.

Syntax:

public void applyPattern(String pattern)

Parameters: The method takes one parameter pattern of String type and refers to the pattern which will be used to format the DecimalPattern.

Return Value: The method returns void type.

Below programs illustrate the working of applyPattern() Method:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate the
// applyPattern method
  
import java.text.DecimalFormat;
  
public class Main {
    public static void main(String[] args)
    {
  
        DecimalFormat deciFormat = new DecimalFormat();
  
        deciFormat.applyPattern("##, ##");
        System.out.println(deciFormat.format(-123456789.54321));
    }
}

chevron_right


Output:

-1, 23, 45, 67, 90

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate the
// applyPattern method
  
import java.text.DecimalFormat;
  
public class Main {
    public static void main(String[] args)
    {
  
        DecimalFormat deciFormat = new DecimalFormat();
  
        deciFormat.applyPattern("#, #00.0#");
        System.out.println(deciFormat.format(-123456789.54321));
    }
}

chevron_right


Output:

-123, 456, 789.54

Reference: https://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html#https://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html#applyPattern(java.lang.String)(java.lang.String)



My Personal Notes arrow_drop_up

The function of education is to teach one to think intensively and to think critically

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.