The NumberFormat.getPercentInstance()
method returns a percentage format for the specified locale. In this example we will format the number as percentage using the Locale.US
locale.
package org.kodejava.text;
import java.text.NumberFormat;
import java.util.Locale;
public class LocalePercentageFormat {
public static void main(String[] args) {
// Format percentage for Locale.US locale with this formatter,
// a decimal fraction such as 0.75 is displayed as 75%
double number = 0.25;
NumberFormat format = NumberFormat.getPercentInstance(Locale.US);
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%
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024