Java Data Type How to - Display a percentage








Question

We would like to know how to display a percentage.

Answer

import java.text.DecimalFormat;
// w w  w  .j a va 2s  .  c o  m
public class Main {
  public static void main(String[] argv) throws Exception {

    DecimalFormat df = new DecimalFormat("#%");
    System.out.println(df.format(0.19));
    System.out.println(df.format(-0.19));
    System.out.println(df.format(-0.009));
    System.out.println(df.format(-12.009));
  }
}

The code above generates the following result.