Java Utililty Methods Number Format

List of utility methods to do Number Format

Description

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

Method

StringformatNumber(double dd)
Write the number in bbbb.bb format needed for currency.
String str = "" + dd;
String ret = "";
int l = str.length();
int i = str.indexOf('.');
int r = i + 3; 
if (i > -1 && r < l) {
    ret = str.substring(0, r);
} else {
...
StringformatNumber(double dscore)
format Number
long score = Math.round(dscore * 100);
return score / 100 + "." + (score % 100 < 10 ? "0" : "") + score % 100;
StringformatNumber(double number, int decimalPlaces)
Format the given number as a String, including the given number of decimal places.
String s = String.valueOf(number);
int idx1 = s.indexOf('.');
if (idx1 == -1) {
    return s;
} else {
    int idx2 = s.indexOf('E');
    int dp = decimalPlaces + (idx2 >= 0 ? 0 : 1);
    String t = s.substring(0, Math.min(idx1 + dp, s.length()));
...
StringformatNumber(Double val, int numDec, int width)
format Number
if (val == null) {
    return String.format("%" + width + "s", UNKNOWN_VALUE);
} else {
    return String.format("%" + width + "." + numDec + "f", val);
StringformatNumber(double value)
format Number
if (value < 1000D)
    return String.valueOf(value);
else if (value < 1000000D)
    return String.valueOf(Math.round(value) / 1000D) + "K";
else if (value < 1000000000D)
    return String.valueOf(Math.round(value / 1000D) / 1000D) + "M";
else if (value < 1000000000000D)
    return String.valueOf(Math.round(value / 1000000D) / 1000D) + "B";
...
StringformatNumber(double value)
format Number
if (value < 1000D)
    return String.valueOf(value);
else if (value < 1000000D)
    return String.valueOf(Math.round(value) / 1000D) + "K";
else if (value < 1000000000D)
    return String.valueOf(Math.round(value / 1000D) / 1000D) + "M";
else if (value < 1000000000000D)
    return String.valueOf(Math.round(value / 1000000D) / 1000D) + "B";
...
StringformatNumber(final long number)
Formats the given number into one where thousands are separated by a space.
long divided = number;
final StringBuffer sb = new StringBuffer();
while (divided > 1000) {
    long rest = divided % 1000;
    sb.insert(0, " " + String.format("%03d", rest));
    divided = divided / 1000;
long rest = divided % 1000;
...
StringformatNumber(final String number)
format Number
String num = "";
String decimal = "";
int index = number.lastIndexOf('.');
if (index >= 0) {
    num = number.substring(0, index);
    decimal = number.substring(index);
} else {
    num = number;
...
voidformatNumber(final StringBuilder buf, final long number)
format Number
if (number < 10) {
    buf.append("0");
buf.append(number);
StringformatNumber(float num, int fragmentSize)
fragmentSize must greater 1.
String ret = Float.toString(num);
int len;
if ((len = ret.indexOf(".")) != -1 && len + fragmentSize + 1 < ret.length()) {
    ret = ret.substring(0, len + fragmentSize + 1);
return ret;