Java Fraction Format formatDecimals(double d, int mantissa)

Here you can find the source of formatDecimals(double d, int mantissa)

Description

Change the precision of a double value, according to the new mantissa.

License

Open Source License

Parameter

Parameter Description
d The double value you want re-precisioned.
mantissa The number of digits after the decimal place.

Declaration

public static double formatDecimals(double d, int mantissa) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.text.DecimalFormat;

public class Main {
    /**//w w  w  .j  a v  a2 s . co m
     * Change the precision of a double value, according to the new mantissa.
     * 
     * @param d
     *            The double value you want re-precisioned.
     * @param mantissa
     *            The number of digits after the decimal place.
     * @return
     */
    public static double formatDecimals(double d, int mantissa) {
        String m = "";
        for (int i = 0; i < mantissa; i++) {
            m += "#";
        }
        DecimalFormat decimalForm = new DecimalFormat("#." + m);
        return Double.valueOf(decimalForm.format(d));
    }
}

Related

  1. formatDecimal(double value)
  2. formatDecimal(double value, int maxFractionDigits1, int maxFractionDigits2)
  3. formatDecimal(double value, int precision)
  4. formatDecimal(double value, String mask)
  5. formatDecimal(String format, double value)
  6. formatDecimals(double number)
  7. formatDecNumber(Double number, String pattern)
  8. formatDegToDms(double degrees, double min, double max)
  9. formatDisplay(Double value)