Java Utililty Methods Decimal Format

List of utility methods to do Decimal Format

Description

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

Method

StringtoString(double value)
to String
if (Double.isNaN(value)) {
    return "NaN";
BigDecimal bigDecimal = new BigDecimal(value);
return bigDecimal.toString();
StringtoString(double value, int precision)
Thread-safe version of converting a double value to a String when only a certain number of digits are desired after a decimal point.
StringBuilder buf = new StringBuilder("0");
if (precision > 0) {
    buf.append(".");
for (int i = 0; i < precision; i++) {
    buf.append("0");
DecimalFormat format = new DecimalFormat(buf.toString());
...
StringtoString(double[] array)
to String
String display = "[";
for (double val : array) {
    display += val + ", ";
display = display.substring(0, display.length() - 2);
display += "]";
return display;
StringtoString(Double[] input)
to String
StringBuilder ret = new StringBuilder();
for (Double o : input) {
    ret.append("");
    if (o != null)
        ret.append("" + dcf.format(o));
    else
        ret.append("NULL");
    ret.append(" ");
...
StringtoString(double[][] a, int decimals)
to String
if (decimals < 0)
    throw new IllegalArgumentException("Number of decimals must be positive");
StringBuilder decs = new StringBuilder("0.");
for (int i = 0; i < decimals; i++)
    decs.append("0");
NumberFormat decimalsFormat = new DecimalFormat(decs.toString());
StringBuilder sb = new StringBuilder();
for (int i = 0; i < a.length; i++) {
...
StringtoString(final Double value)
to String
if (Double.isNaN(value) || Double.isInfinite(value)) {
    return "U";
} else {
    return formatters.get().format(value);
StringtoString(final double value, final int maxDecimalPlaces)
to String
DECIMAL_FORMAT.setMaximumFractionDigits(maxDecimalPlaces);
return DECIMAL_FORMAT.format(value);
StringtoStringForHumans(double doubleToConvert)
to String For Humans
NumberFormat formatter = NumberFormat.getInstance();
return formatter.format(doubleToConvert);
StringtoStringNoDigits(double[] v)
to String No Digits
NumberFormat nf = new DecimalFormat("0");
String res = "";
for (int i = 0; i < v.length; i++) {
    res += (i > 0 ? "," : "") + nf.format(v[i]);
return res + "";
StringtoStringScientific(double x)
Scientific format
return scientific_.format(x);