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

Stringformat(Double number)
format
String num = String.valueOf(number);
int index = num.indexOf(".");
num = num.substring(0, index);
return num;
Stringformat(double number, int nums)
format
return String.format("%." + nums + "f", number);
doubleformat(double number, int precision)
format
int tmp = 1;
for (int i = 0; i < precision; i++) {
    tmp *= 10;
int value = (int) Math.round(number * tmp);
return (value * 1d) / tmp;
Stringformat(double size, String type)
format
int precision = 0;
if (size * 1000 % 10 > 0) {
    precision = 3;
} else if (size * 100 % 10 > 0) {
    precision = 2;
} else if (size * 10 % 10 > 0) {
    precision = 1;
} else {
...
Stringformat(double val, int n, int w)
format
double incr = 0.5;
for (int j = n; j > 0; j--) {
    incr /= 10;
val += incr;
String s = Double.toString(val);
final int n1 = s.indexOf('.');
final int n2 = s.length() - n1 - 1;
...
Stringformat(double value, int decimalPlaces)
format
if (decimalPlaces == 0) {
    return format(Math.round(value));
final long integerPart = (long) value;
double adjust = Math.pow(10, decimalPlaces);
long decimalPart = Math.round((Math.abs(value) * adjust) % adjust) + (long) adjust;
return format(integerPart) + "." + Long.toString(decimalPart).substring(1);
doubleformat(double value, int digits)
format
if (digits < 0) {
    return value;
return Double.valueOf(String.format("%." + digits + "f", value));
Stringformat(final double power)
format
return power == 0 ? "%.0f%s" : "%.1f%s";
StringformatDouble(double d)
Format a double into a string using #DECIMAL_FORMAT .
return String.format(DECIMAL_FORMAT, d);
StringformatDouble(double d)
format Double
String s = "" + d;
if (s.endsWith(".0"))
    s = s.substring(0, s.length() - 2);
return s;