Java Fraction Format formatDouble(Double in)

Here you can find the source of formatDouble(Double in)

Description

convert a double into a String with a given precision default double formatting is not very pretty This version defaults precision to 2

License

Apache License

Parameter

Parameter Description
in positive precision

Return

non-null formatted string

Declaration

public static String formatDouble(Double in) 

Method Source Code

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

import java.text.*;

public class Main {
    /**//from w  ww  .j a  va  2s.c om
     * convert a double into a String with a given precision
     * default double formatting is not very pretty
     * @param in non-null Double to convert
     * @param int positive precision
     * @return non-null formatted string
     */
    public static String formatDouble(Double in, int precision) {
        return (formatDouble(in.doubleValue(), precision));
    }

    /**
     * convert a double into a String with a given precision
     * default double formatting is not very pretty
     * @param in double to convert
     * @param int positive precision
     * @return non-null formatted string
     */
    public static String formatDouble(double in, int precision) {
        NumberFormat nf = NumberFormat.getInstance();
        nf.setMaximumFractionDigits(precision);
        return (nf.format(in));
    }

    /**
     * convert a double into a String with a given precision
     * default double formatting is not very pretty
     * This version defaults precision to 2
     * @param in non-null Double to convert
     * @param in positive precision
     * @return non-null formatted string
     */
    public static String formatDouble(Double in) {
        return (formatDouble(in.doubleValue()));
    }

    /**
     * convert a double into a String with a given precision
     * default double formatting is not very pretty
     * This version defaults precision to 2
     * @param in double to convert
     * @return non-null formatted string
     */
    public static String formatDouble(double in) {
        return (formatDouble(in, 2));
    }
}

Related

  1. formatDouble(double d)
  2. formatDouble(Double d)
  3. formatDouble(double d, int maximumFractionDigits)
  4. formatDouble(Double d, NumberFormat format)
  5. formatDouble(Double doubleValue)
  6. formatDouble(double inVal, int inNumPlaces, boolean pad)
  7. formatDouble(Double num)
  8. formatDouble(double num)
  9. formatDouble(Double num)