Java Utililty Methods Number to String

List of utility methods to do Number to String

Description

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

Method

StringnumberToString(double num, int digits)
number To String
if (digits < 0)
    throw new RuntimeException("Digits must be >= 0!");
String[] parts = String.valueOf(num).split("[.]");
if (parts[1].matches("0"))
    return parts[0];
if (parts[1].length() > digits) {
    String end = parts[1].substring(0, digits);
    while (end.endsWith("0"))
...
StringnumberToString(int gmNumber)
Translates the General-Midi-Instrument-Number into a human readable string.
if ((gmNumber >= 0) && (gmNumber < instrumentNames.length)) {
    return instrumentNames[gmNumber];
return "" + gmNumber;
StringnumberToString(int number, int length, String type, int location)
number To String
String numberStr = String.valueOf(number);
if (numberStr.length() == length) {
    return numberStr;
} else if (numberStr.length() > length) {
    return numberStr.substring(numberStr.length() - length);
StringBuffer value = new StringBuffer();
for (int i = 0; i < length - numberStr.length(); i++) {
...
StringnumberToString(Number number)
Produce a string from a Number.
if (number == null) {
    return "null";
if (!testValidity(number)) {
    throw new RuntimeException("Can't convert number: " + number);
String string = number.toString();
if (string.indexOf('.') > 0 && string.indexOf('e') < 0 && string.indexOf('E') < 0) {
...
StringnumberToString(Object instance)
number To String
String s = instance.toString();
int i = s.lastIndexOf(".");
if (i == -1) {
    return s;
String result = s.substring(0, i);
return result;
StringnumberToString(Object num)
number To String
if (num == null) {
    return null;
} else if (num instanceof Integer && (Integer) num > 0) {
    return Integer.toString((Integer) num);
} else if (num instanceof Long && (Long) num > 0) {
    return Long.toString((Long) num);
} else if (num instanceof Float && (Float) num > 0) {
    return Float.toString((Float) num);
...
StringnumberToStringFlushLeft(long number, int width)

Returns a long value as a string, inserts spaces on the left so that the specified width is reached.

return flushLeft(Long.toString(number), width);