Java Fraction Format formatDoubleAsString(Double val)

Here you can find the source of formatDoubleAsString(Double val)

Description

format Double As String

License

Open Source License

Parameter

Parameter Description
val a parameter

Declaration

public static String formatDoubleAsString(Double val) 

Method Source Code

//package com.java2s;
import java.text.DecimalFormat;

public class Main {
    protected static final DecimalFormat decimalFormat = new DecimalFormat();

    /**/* w w  w .j  a  v  a 2  s  .  c o  m*/
     * @param val
     * @return
     */
    public static String formatDoubleAsString(Double val) {
        return formatDoubleAsString(val, 2);
    }

    /**
     * @param val
     * @param decimalPlaces
     * @return
     */
    public static String formatDoubleAsString(Double val, int decimalPlaces) {
        if (val != null) {
            if (decimalFormat.getMaximumFractionDigits() != decimalPlaces) {
                decimalFormat.setMinimumFractionDigits(decimalPlaces);
                decimalFormat.setMaximumFractionDigits(decimalPlaces);
            }
            return decimalFormat.format(val);
        }
        return "";
    }
}

Related

  1. formatDouble(Object obj)
  2. formatDouble(Object value)
  3. formatDoubleAmount(double amount)
  4. formatDoubleArray(double[] arr, String format)
  5. formatDoubleAsPointsString(Double d)
  6. formatDoubleDecimal(float in)
  7. formatDoubleDecPoint2(Double value)
  8. formatDoubleList(List list)
  9. formatDoubleString(double data, String pattern)