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(long value)
Formats an integer as a string
return String.format("%,d", value);
StringformatNumber(Number number)

Format a Number into a String making sure that NullPointerException s are not thrown.

if (number == null)
    return null;
return (number.toString());
StringformatNumber(Object num)
format Number
if (num == null)
    return null;
String number = num.toString().trim();
if (number.length() == 0)
    return null;
if (number.indexOf(".") == -1)
    return number;
return number.length() - number.indexOf(".") > 2 ? number.substring(0, number.indexOf(".") + 3) : number;
...
StringformatNumber(String formatStr, int length, boolean leftPadding, char paddingCharacter)
format Number
StringBuilder result = new StringBuilder(formatStr);
String paddingStr = String.valueOf(paddingCharacter);
while (result.length() < length) {
    if (leftPadding) {
        result = (new StringBuilder(paddingStr)).append(result);
    } else {
        result.append(paddingCharacter);
return result.toString();
StringformatNumber(String num, int n)
format Number
if (num != null) {
    while (num.length() < n) {
        num = "0" + num;
return num;
StringformatNumber(String s, Character thousandsSeparator, Character decimalSeparator)
in order to transform the number "1234567.89" to string 123,456.89
if (s == null) {
    return null;
String result = s;
int decimalIndex = s.indexOf("."); 
if (decimalIndex == -1) {
    if (thousandsSeparator != null) {
        return formatNumber(result, thousandsSeparator);
...
StringformatNumber(String value)
format Number
if (value == null)
    return null;
return value.replace(",", ".");
StringformatNumberFloorWithPostfix(int value)
Formats the number in value into a floored, postfixed form for display.
if (value >= 1000000000) {
    return String.format("%dG", value / 1000000000);
if (value >= 1000000) {
    return String.format("%dM", value / 1000000);
if (value >= 10000) {
    return String.format("%dk", value / 1000);
...
StringformatNumberImpl(long i, int digits, String fill)
format Number Impl
int ten = 10;
String res = "";
while (digits > 1) {
    if (i < ten) {
        res += fill;
    digits--;
    ten *= 10;
...
StringformatNumbers(final int... numbers)
Returns a formatted string to describe the supplied numbers.
final StringBuilder sb = new StringBuilder();
if (numbers.length == 1) {
    sb.append(numbers[0]).append(" ");
} else {
    sb.append("( ");
    for (int number : numbers) {
        sb.append(number).append(" ");
    sb.append(") ");
return sb.toString();