Java Utililty Methods Double Number Format

List of utility methods to do Double Number Format

Description

The list of methods to do Double Number Format are organized into topic(s).

Method

StringformatDouble(double val)
format Double
String num = String.format("%.2f", val);
if (!num.startsWith("-")) {
    return pad(" " + num, 6);
} else {
    return pad(num, 6);
StringformatDouble(double val)
format Double
String out = String.valueOf(val);
if (out.length() > 5) {
    boolean round = false;
    if (round && out.charAt(5) != '.')
        if (Integer.parseInt(String.valueOf(out.charAt(5))) > 4)
            round = true;
    out = out.substring(0, 5);
    if (round && out.charAt(4) != '.')
...
StringformatDouble(double val, int prec)
format Double
String formatter = "%." + prec + "f";
return String.format(formatter, val);
StringformatDouble(double val, int precision)
format Double
String format_string = "%1$." + precision + "f";
return String.format(format_string, val);
StringformatDouble(double value)
This method takes double value as a parameter and formats it to two decimal points and returns the formatted value.
double valueHundred = roundDouble(value) * 100;
Double temp = new Double(roundDouble(valueHundred));
String cost = String.valueOf(temp.longValue());
if (cost.equals("0")) {
    cost = "0.00";
} else {
    cost = cost.substring(0, cost.length() - 2) + "." + cost.substring(cost.length() - 2);
return cost;
StringformatDouble(double value)
Format the given double with four digits precision, but leave it without a decimal point.
if (equalsEps(Math.round(value), value)) {
    return Long.toString(Math.round(value));
return Double.toString(Math.round(value * 10000.0) / 10000.0);
StringformatDouble(double value)
Formats a value with two decimals, using spaces to separate thousands as: ddd ddd ddd.dd.
boolean isNegative = value < 0.0 || (value == 0.0 && 1 / value < 0.0);
if (isNegative) {
    value = -value;
char[] buffer = new char[1 + 19 + 6 + 3];
long intValue = (long) value;
int decValue = (int) ((value - intValue) * 100 + 0.5);
int index = buffer.length - 1;
...
StringformatDouble(double value)
format Double
return truncate("" + value, 4);
StringformatDouble(double value, int decimals)
format Double
String doubleStr = "" + value;
int index = doubleStr.indexOf(".") != -1 ? doubleStr.indexOf(".") : doubleStr.indexOf(",");
if (index == -1)
    return doubleStr;
if (decimals == 0) {
    return doubleStr.substring(0, index);
int len = index + decimals + 1;
...
StringformatDouble(final double value)
format Double
return String.format(DOUBLE_FORMAT, value);