Java - Write code to calculate Percentage

Requirements

Write code to calculate Percentage

Demo

//package com.book2s;

import java.text.DecimalFormat;

public class Main {
    public static void main(String[] argv) {
        double x = 4;
        double total = 42.45678;
        System.out.println(getPercent(x, total));
    }/*from w w  w.  j  av a  2  s.  c  om*/

    public static String getPercent(double x, double total) {
        if (total == 0 || x == 0) {
            return "0%";
        }
        double tempresult = x / total;
        DecimalFormat df1 = new DecimalFormat("0.0%"); // ##.00%
        String result = df1.format(tempresult);
        return result;
    }
}