get Format Decimal Amount - Android java.lang

Android examples for java.lang:Double

Description

get Format Decimal Amount

Demo Code


public class Main {

  /**//from w  ww.ja  v  a2  s.c  om
   * return formatted float as 232.00000000 should be 232, 0.180000000001 should be
   * 0.18
   * 
   * @param pInputFloat
   * @return
   */
  public static String getFormatDecimalAmount(float pInputFloat) {
    return getFormatDecimalAmount(pInputFloat, 2);
  }

  /**
   * return formatted float as 232.00000000 should be 232, 0.180000000001 should be
   * 0.18
   * 
   * @param pInputFloat
   * @param pNeededDigitsAfterDecimal
   * @return
   */
  public static String getFormatDecimalAmount(float pInputFloat, int pNeededDigitsAfterDecimal) {
    if (pInputFloat == (int) pInputFloat || pNeededDigitsAfterDecimal <= 0) {
      return String.format("%d", (int) pInputFloat);
    } else {
      return String.format("%1." + pNeededDigitsAfterDecimal + "f", pInputFloat);
    }
  }

}

Related Tutorials