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

Stringformat(int decs, double number)
Formats a number to a given amount of decimal places.
return String.format("%." + decs + "f", number);
Stringformat(int number, int length)
format
String numberString = Integer.toString(number);
char[] erg = new char[length];
for (int i = 0; i < length; i++) {
    erg[i] = '0';
char[] numberStringChars = numberString.toCharArray();
for (int i = 0; i < numberString.length(); i++) {
    erg[length - 1 - i] = numberStringChars[numberString.length() - 1 - i];
...
Stringformat(Number number)
Returns a formatted string of the specified number.
String formatted = Double.toString(number.doubleValue()).replaceAll("\\.0+$", "") 
        .replaceAll("(\\.[0-9]*[1-9])0+$", "$1"); 
return formatted;
Stringformat(String number)
format
StringBuilder cleanedNumber = new StringBuilder();
for (int index = 0; index < number.length(); index++) {
    char c = number.charAt(index);
    if (c == '+' || c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || c == '6'
            || c == '7' || c == '8' || c == '9')
        cleanedNumber.append(c);
return cleanedNumber.toString();
...
Doubleformat(String number)
format
Double ret = null;
String work = number.replace(',', '.');
ret = new Double(work);
return ret;
StringformatAmountDigits(String numberString, int length)
format Amount Digits
while (numberString.length() < length) {
    numberString = "0" + numberString;
return numberString;
byte[]formatBinaryNumber(int number, int size)
format Binary Number
byte[] bin = new byte[size];
int work = number;
for (int i = (size - 1); i >= 0; i--) {
    int b = (work & 0xff);
    bin[i] = (byte) b;
    work = (work >> 8);
return bin;
...
StringformatBytes(long number)
format Bytes
float div = 1024;
float start = number;
int pfix = 0;
while (start > div) {
    start = start / div;
    pfix++;
start = ((long) (start * 10)) / 10f;
...
StringformatBytesHumanReadable(float number)
format Bytes Human Readable
String unit = "bytes";
if (number > 1024) {
    number /= 1024;
    unit = "KiB";
if (number > 1024) {
    number /= 1024;
    unit = "MiB";
...
StringformatCardNumber(String cardNumber)
format Card Number
StringBuilder formatted = new StringBuilder();
if (cardNumber.length() == 10 && cardNumber.startsWith("B")) {
    formatted.append(cardNumber.substring(0, 1));
    formatted.append(' ');
    formatted.append(cardNumber.substring(1, 8));
    formatted.append(' ');
    formatted.append(cardNumber.substring(8));
} else if (cardNumber.length() == 12) {
...