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(final Object value)
Format double.
return null;
DoubleformatDouble(String value)
Checks if the value can safely be converted to a double primitive.
if (value == null) {
    return null;
try {
    return new Double(value);
} catch (NumberFormatException e) {
    return null;
StringformatDoubleAsString(double num, int n)
format Double As String
String ret = String.format("%." + n + "f", num).replace(',', '.');
if (ret.length() > 5) {
    ret = String.format("%5.2e", Double.parseDouble(ret));
return ret;
voidformatDoubleFast(double source, int decimals, int precision, StringBuffer target)
Rounds the given source value at the given precision and writes the rounded value into the given target

This method internally uses double precision computation and rounding, so the result may not be accurate (see formatDouble method for conditions).

if (isRoundedToZero(source, decimals, precision)) {
    target.append('0');
    return;
} else if (Double.isNaN(source) || Double.isInfinite(source)) {
    target.append(Double.toString(source));
    return;
boolean isPositive = source >= 0.0;
...
StringformatDoubleForDecimalPlaces(double d, int decimalcount)
Utility function to format a given double to the accuracy of decimalcount decimal places.
long intpredecimals = (long) d;
double tenexp = Math.pow(10, decimalcount);
double decimals = d - intpredecimals;
double postdecimals = decimals * (long) tenexp;
long intpostdecimals = (long) postdecimals;
if ((postdecimals - intpostdecimals) >= 0.5) {
    intpostdecimals++;
return intpredecimals + "." + intpostdecimals;
DoubleformatDoubleInfinity(Double d)
format Double Infinity
if (d == null)
    return null;
if (d.equals(Double.POSITIVE_INFINITY)) {
    return Double.MAX_VALUE;
} else if (d.equals(Double.NEGATIVE_INFINITY)) {
    return Double.MIN_VALUE;
} else if (d.equals(Double.NaN)) {
    return null;
...
voidformatDoublePrecise(double source, int decimals, int precision, StringBuffer target)
Rounds the given source value at the given precision and writes the rounded value into the given target

This method internally uses the String representation of the source value, in order to avoid any double precision computation error.

if (isRoundedToZero(source, decimals, precision)) {
    target.append('0');
    return;
} else if (Double.isNaN(source) || Double.isInfinite(source)) {
    target.append(Double.toString(source));
    return;
boolean negative = source < 0.0;
...
StringformatDoubleToFixedLength(String value, int length)
Right justified with 2 decimal places.Procced with zeros?
String valueStr = value + "";
int diff = length - valueStr.length();
StringBuilder strBuilder = new StringBuilder();
if (diff > 0) {
    while (diff > 0) {
        diff--;
        strBuilder.append(" ");
    strBuilder.append(valueStr);
    valueStr = strBuilder.toString();
return valueStr;
StringformatDoubleToString(double d)
format Double To String
if (d == (long) d)
    return String.format("%d", (long) d);
else
    return String.format("%s", d);
StringformatDoubleToString(double n)
format Double To String
String s = String.format(format, n);
while (s.charAt(s.length() - 1) == '0') {
    s = s.substring(0, s.length() - 1);
if (s.charAt(s.length() - 1) == '.') {
    s = s.substring(0, s.length() - 1);
return s;
...