Java NumberFormat getPercentageInstance Method



In this article, we will learn the NumberFormat.getPercentageInstance() method. Java provides powerful tools for formatting numbers, currencies, and percentages through the java.text.NumberFormatclass. One of the most useful methods in this class is the getPercentageInstance() method.

NumberFormat.getPercentageInstance() method

The NumberFormat.getPercentageInstance() method is a static method that returns a NumberFormat instance configured to format numbers as percentages. This method can be used with or without a Locale parameter. 

Syntax ?

public static NumberFormat getPercentageInstance()
public static NumberFormat getPercentageInstance(Locale locale)
  • Without Locale: If no locale is specified, the method uses the default locale of the JVM.
  • With Locale: You can pass a `Locale` object to format the percentage according to the conventions of that locale.

Formatting Percentages in Java

In this example, we will format a percentage using the Locale.FRANCE, which represents the French locale.

Locale-Specific Formatting: The getPercentageInstance(Locale) method ensures that the percentage is formatted according to the conventions of the specified locale. For example, in France, the percentage symbol is placed after the number, and a non-breaking space is used as the thousand separator.

NumberFormat n = NumberFormat.getPercentInstance(Locale.FRANCE);

Example

Below is an example of formatting percentages in the French Locale using the getPercentageInstance() method ?

import java.text.NumberFormat;
import java.util.Locale;
public class MainClass {
   public static void main(String[] args) {
       // Currency of France is Euro
       NumberFormat n = NumberFormat.getPercentInstance(Locale.FRANCE);
       // points
       double points = 1.78;
       double totalPoints = points * 1000;
       System.out.println(n.format(points));
       System.out.println(n.format(totalPoints));
    }
}

Output

178 %
178 000 %

Formatting Percentages in the US Locale

To further illustrate the working of the getPercentageInstance() method, we use the US locale for explaining the getPercentageInstance in Java.

Locale-Specific Formatting: In the US locale, the percentage symbol is placed immediately after the number, and a comma is used as the thousand separator.

NumberFormat n = NumberFormat.getPercentInstance(Locale.US);

Example

Below is an example of formatting percentages in the US Locale using the getPercentageInstance() method ?

import java.text.NumberFormat;
import java.util.Locale;

public class MainClass {
    public static void main(String[] args) {
        // Create a NumberFormat instance for percentages using the US locale
        NumberFormat n = NumberFormat.getPercentInstance(Locale.US);
        
        // Define some points
        double points = 0.85;
        double totalPoints = points * 1000;
        
        // Format and print the points as percentages
        System.out.println(n.format(points));       // Output: 85%
        System.out.println(n.format(totalPoints));  // Output: 85,000%
    }
}

Output

85% 
85,000%

Conclusion

The NumberFormat.getPercentageInstance() method is a powerful tool for formatting numbers as percentages in Java. By using this method, developers can ensure that their applications display percentages in a way that is consistent with the conventions of the user's locale. Whether you're working with small percentages or large ones, this method provides a simple and effective way to format your data.

Updated on: 2025-01-28T14:58:02+05:30

550 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements