Java Percentage Format toPercent(double value)

Here you can find the source of toPercent(double value)

Description

to Percent

License

Open Source License

Declaration

public static String toPercent(double value) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.text.Collator;

public class Main {
    public static String toPercent(double value) {
        return toPercent(value * 100.0, 100.0);
    }//  w  w  w. j a va 2s. c  om

    public static String toPercent(double value, double total) {
        double percent = value / total;
        percent = percent * 100;
        String result = String.valueOf(percent);
        result = result.substring(0, result.indexOf(".") + 2);
        return result + "%";
    }

    public static int indexOf(String[] smaller, String[] bigger, Collator collator) {
        if (smaller.length > bigger.length) {
            return -1;
        }
        for (int i = 0; i < bigger.length; i++) {
            if (foundAt(smaller, bigger, collator, i)) {
                return i;
            }
        }
        return -1;
    }

    private static boolean foundAt(String[] smaller, String[] bigger, Collator collator, int offset) {
        if (smaller.length + offset > bigger.length) {
            return false;
        }

        int j = 0;
        for (int i = offset; i < bigger.length; i++) {
            if (j + 1 > smaller.length) {
                return true;
            }
            String fromBigger = bigger[i];
            String fromSmaller = smaller[j];
            if (collator.compare(fromBigger, fromSmaller) != 0) {
                return false;
            }
            j++;
            continue;
        }

        return true;
    }
}

Related

  1. percentFormat(Object object)
  2. percentTotal(double value, double total)
  3. roundedPercentageGreaterThan(double left, double right)
  4. toPercent(Double doubleValue)
  5. toPercent(double percent)
  6. toPercent(float f)