Java Double Number Format doubleFormat(Double d)

Here you can find the source of doubleFormat(Double d)

Description

Formats a double to two decimal places

License

Apache License

Parameter

Parameter Description
d Double to format

Return

Formatted double

Declaration

public static String doubleFormat(Double d) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.text.NumberFormat;

public class Main {
    /**//from  www  . j a v a  2s .  c  o  m
     * Formats a double to two decimal places
     *
     * @param d Double to format
     * @return Formatted double
     */
    public static String doubleFormat(Double d) {
        return doubleFormat(d, 0);
    }

    public static String doubleFormat(double d) {
        return doubleFormat(new Double(d));
    }

    public static String doubleFormat(Double d, int minFraction) {
        if (d == null) {
            return null;
        }
        NumberFormat nf = NumberFormat.getInstance();
        nf.setMaximumFractionDigits(2);
        if (minFraction > 0) {
            nf.setMinimumFractionDigits(minFraction);
        }
        return nf.format(d);
    }

    public static String doubleFormat(double d, int minFraction) {
        return doubleFormat(new Double(d), minFraction);
    }
}

Related

  1. convertDouble(double damage)
  2. doubleFormat(double d)
  3. doubleFormat(double number)
  4. doubleFormat(double val, boolean hiRes)
  5. format(double b)
  6. format(double d, int decimals)