Java Number Power powerString(int power)

Here you can find the source of powerString(int power)

Description

power String

License

Open Source License

Declaration

public static String powerString(int power) 

Method Source Code

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

public class Main {
    public static String powerString(int power) {
        float scaledPower = Math.abs(power);
        String unit;/*from  w ww . ja va  2  s. c  o m*/
        if (power >= 1000000) {//1 million Endergy = 1mE (megaEndergy)
            scaledPower /= 1000000;
            unit = "mE";
        } else if (power >= 1000) {//1 thousand Endergy = 1kE (kiloEndergy)
            scaledPower /= 1000;
            unit = "kE";
        } else {
            unit = "E";
        }
        String string = (power < 0 ? "-" : "") + String.valueOf(scaledPower);
        if (scaledPower == Math.floor(scaledPower) && string.contains(".")) {//If it's a whole number, just return the whole part.
            string = string.substring(0, string.indexOf('.'));
        } else {
            if (string.length() > 4)
                string = string.substring(0, 4);//Truncate to two decimal points
        }
        return string + unit;
    }
}

Related

  1. powerOfN(Double v, int n)
  2. powerOfTen(int exp)
  3. powerOfTwo(final int i)
  4. powerOfTwo(int current, int desired)
  5. PowerOutageDistr()
  6. powerString(int power)
  7. powerToInteger(String pow)
  8. powerTrim(String s)
  9. powerType(float power)