How do I format number as percentage string?

The NumberFormat.getPercentInstance() method returns a percentage format for the specified locale. In this example we will format the number as percentage using the Arabic locale.

package org.kodejava.example.text;

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

public class LocalePercentageFormat {
    public static void main(String[] args) {
        //
        // Create a Locale object for Arabic
        //
        Locale locale = new Locale("ar");

        //
        // Format percentage for Arabic locale with this formatter,
        // a decimal fraction such as 0.75 is displayed as 75%
        //
        double number = 0.25;
        NumberFormat format = NumberFormat.getPercentInstance(locale);
        String percentage = format.format(number);
        System.out.println(number + " in percentage = " + percentage);
    }
}

The following line is the output of the program:

0.25 in percentage = 25%
Wayan Saryada

Wayan Saryada

A programmer, runner, recreational diver, currently living in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, JPA, etc. If you need help on Java programming you can hire me on Fiverr.
Wayan Saryada

Leave a Reply