Java method for calculating percentage values

  • 2020-04-01 03:44:29
  • OfStack

This article illustrates a Java method for calculating percentage values. Share with you for your reference. The specific implementation method is as follows:


public class Test1 { 
  public static String myPercent(int y, int z) { 
    String baifenbi = "";//Accept the percentage value
    double baiy = y * 1.0; 
    double baiz = z * 1.0; 
    double fen = baiy / baiz; 
//NumberFormat nf = NumberFormat. GetPercentInstance (); Comment out is another way
//Nf. SetMinimumFractionDigits (2); Save it for a few decimal places
    DecimalFormat df1 = new DecimalFormat("##.00%");
// ##.00% 
//Percentage format, after less than 2 bits with 0 complement
// baifenbi=nf.format(fen); 
    baifenbi = df1.format(fen); 
    System.out.println(baifenbi); 
    return baifenbi; 
  } 
  public static void main(String[] args) {
    myPercent(29,59);
  }
}

I hope this article has been helpful to your Java programming.


Related articles: