Convert decimal value To Percent - Android java.lang

Android examples for java.lang:Double

Description

Convert decimal value To Percent

Demo Code

import java.text.DecimalFormat;

public class Main {
  public static boolean emptyOrNull(String str) {
    return str == null || str.length() == 0;
  }/*  ww  w  .  jav  a 2 s .co m*/

  public static boolean emptyOrNull(String... arrStr) {
    for (String str : arrStr) {
      if (emptyOrNull(str)) {
        return true;
      }
    }
    return false;
  }

  public static String decimalToPercent(String decimal) {
    float value = -1;
    if (!emptyOrNull(decimal)) {
      try {
        value = Float.parseFloat(decimal);
      } catch (NumberFormatException e) {
      }
    }
    if (value == -1) {
      return "";
    } else {
      DecimalFormat format = new DecimalFormat("0.#%");
      return format.format(value);
    }
  }

}

Related Tutorials