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

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

Description

double To Fixed String

License

Open Source License

Declaration

public static String doubleToFixedString(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 {
    public static String doubleToFixedString(double d, int length) {
        String s = doubleToAtLeastString(d, length);
        if (s.charAt(0) != '-') {
            return " " + s.substring(0, length - 1);
        }/* w w w .ja v a2 s  .  com*/
        return s.substring(0, length);
    }

    /**
     * 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 value)
  2. doublesToStrings(final double... numbers)
  3. doubleToAtLeastString(double d, int length)
  4. doubleToCoreString(double number)
  5. doubleToDollarString(double val)
  6. doubleToScientificString(double number, int fractionDigits)
  7. doubleToStr(double d)
  8. doubleToStr(double origin, int decimals)
  9. doubleToStr1(double d)