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

doubleformatDouble(double d)
format Double
return (double) Math.round(d * 100) / 100;
StringformatDouble(double d, int n)
format Double
int x = (int) Math.pow(10, n);
return Double.toString((double) (((int) (x * d)) / (double) x));
StringformatDouble(double d, int n)
Formats a double to a given number of digits past the decimal point and cuts the tailing zeroes
String s = String.format("%." + n + "f", d);
int i = s.length() - 1;
while (s.charAt(i) == '0') {
    s = s.substring(0, i);
    i--;
if (s.charAt(i) == '.')
    s = s.substring(0, i);
...
StringformatDouble(double d, int n, String pad)
format Double
String s = Double.toString(d);
int len = s.length();
if (len > n)
    return s.substring(0, n);
for (; len < n; len++)
    s = s + pad;
return s;
StringformatDouble(double d, int precision)
Returns a string of the double with the specified floating point precision with zeroes added if required to achieve the precision.
double 10.03 with precision 4 will result in 10.0300
double 2.123456 with precision 4 will result in 2.1234
String result = String.valueOf(d);
int floatingPoint = result.indexOf('.');
int addZeroes = -(result.length() - (floatingPoint + 1) - precision);
if (addZeroes < 0) {
    return result.substring(0, floatingPoint + 1 + precision);
} else {
    for (int i = 0; i < addZeroes; i++) {
        result = result + "0";
...
StringformatDouble(double inDouble, boolean inComma, int inCommaPos)
This function is to format the double value.
int dp = 0;
long tens = 1;
long tempInt = (long) (fixDouble(inDouble * tens));
while (fixDouble(inDouble * tens) != tempInt) {
    dp++;
    tens = tens * 10;
    tempInt = (long) (fixDouble(inDouble * tens));
return formatDouble(inDouble, dp, inComma, inCommaPos);
StringformatDouble(double num, int width, int precision)
format Double
return formatReal(String.valueOf(num), width, precision); 
StringFormatDouble(double p_value, int p_numberOfDecimalPlaces)
Format Double
String t_valueString = Double.toString(p_value);
if (t_valueString.contains(".")) {
    int t_decimalIndex = t_valueString.indexOf(".");
    if ((t_valueString.length() - (t_decimalIndex + 1)) > p_numberOfDecimalPlaces) {
        t_valueString = t_valueString.substring(0, (t_decimalIndex + (p_numberOfDecimalPlaces + 1)));
return t_valueString;
...
StringformatDouble(double source, int decimals, int precision)
format Double
StringBuffer target = new StringBuffer();
int scale = (Math.abs(source) >= 1.0) ? decimals : precision;
if (tooManyDigitsUsed(source, scale) || tooCloseToRound(source, scale)) {
    formatDoublePrecise(source, decimals, precision, target);
} else {
    formatDoubleFast(source, decimals, precision, target);
return target.toString();
...
StringformatDouble(double v, int decimalPlaces)
format Double
String ret = String.valueOf(v);
int i = ret.indexOf(".");
if (ret.length() > i + decimalPlaces + 1)
    ret = ret.substring(0, i + decimalPlaces + 1);
else
    ret += "0000000000".substring(0, i + decimalPlaces + 1 - ret.length());
return ret;