Java Fraction Format formatDouble(double number, int integerDigits, int fractionDigits)

Here you can find the source of formatDouble(double number, int integerDigits, int fractionDigits)

Description

Returns a formatted string of the given number, with the given numbers of digit space for the integer and fraction parts.

License

Open Source License

Parameter

Parameter Description
number the number to format
integerDigits the length of the integer part
fractionDigits the length of the fraction part

Return

a formatted number

Declaration

public static String formatDouble(double number, int integerDigits, int fractionDigits) 

Method Source Code

//package com.java2s;
/*/*  w  w w  . j  av a 2 s.  co m*/
 * Copyright 1999-2002 Carnegie Mellon University.  
 * Portions Copyright 2002 Sun Microsystems, Inc.  
 * Portions Copyright 2002 Mitsubishi Electric Research Laboratories.
 * All Rights Reserved.  Use is subject to license terms.
 * 
 * See the file "license.terms" for information on usage and
 * redistribution of this file, and for a DISCLAIMER OF ALL 
 * WARRANTIES.
 *
 */

import java.text.DecimalFormat;

public class Main {
    /** DecimalFormat object to be used by all the methods. */
    private static final DecimalFormat format = new DecimalFormat();

    /**
     * Returns a formatted string of the given number, with the given numbers of digit space for the integer and
     * fraction parts. If the integer part has less than <code>integerDigits</code> digits, spaces will be prepended to
     * it. If the fraction part has less than <code>fractionDigits</code>, spaces will be appended to it. Therefore,
     * <code>formatDouble(12345.6789, 6, 6)</code> will give
     * the string <pre>" 12345.6789  "</pre> (one space before 1, two spaces
     * after 9).
     *
     * @param number         the number to format
     * @param integerDigits  the length of the integer part
     * @param fractionDigits the length of the fraction part
     * @return a formatted number
     */
    public static String formatDouble(double number, int integerDigits, int fractionDigits) {
        StringBuilder formatter = new StringBuilder(2 + fractionDigits).append("0.");
        for (int i = 0; i < fractionDigits; i++) {
            formatter.append('0');
        }

        format.applyPattern(formatter.toString());
        String formatted = format.format(number);

        // pad preceding spaces before the number
        int dotIndex = formatted.indexOf('.');
        if (dotIndex == -1) {
            formatted += ".";
            dotIndex = formatted.length() - 1;
        }
        StringBuilder result = new StringBuilder();
        for (int i = dotIndex; i < integerDigits; i++) {
            result.append(' ');
        }
        result.append(formatted);
        return result.toString();
    }
}

Related

  1. formatDouble(Double num)
  2. formatdouble(double number)
  3. formatDouble(double number)
  4. formatDouble(double number)
  5. formatDouble(double number, int decimalDigits)
  6. formatDouble(double orig)
  7. formatDouble(Double someDouble)
  8. formatDouble(Double v)
  9. formatDouble(double value)