Java Double Number to String doubleToAtLeastString(double d, int length)

Here you can find the source of doubleToAtLeastString(double d, int length)

Description

return a string that is at least a certain number of characters in length.

License

Open Source License

Parameter

Parameter Description
d a parameter
length a parameter

Declaration

public static String doubleToAtLeastString(double d, int length) 

Method Source Code


//package com.java2s;
//GNU General Public License (GPL).

import java.text.DecimalFormat;
import java.text.NumberFormat;

public class Main {
    /**//  w  w w.  j  a v a2  s. c om
     * return a string that is at least a certain
     * number of characters in length.  This is helpful
     * when populating text boxes with number data that
     * may otherwise be short and packed too tightly
     * by the GUI components.
     * @param d
     * @param length
     * @return
     */
    public static String doubleToAtLeastString(double d, int length) {
        //String s = String.valueOf(d);
        NumberFormat formatter = new DecimalFormat("0.000");
        String s = formatter.format(d);
        for (int i = s.length(); i < length; i++) {
            s += "0";
        }
        return s;
    }
}

Related

  1. double2String(double d)
  2. double2String(double value)
  3. doublesToStrings(final double... numbers)
  4. doubleToCoreString(double number)
  5. doubleToDollarString(double val)
  6. doubleToFixedString(double d, int length)
  7. doubleToScientificString(double number, int fractionDigits)