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

StringformatPhoneNo(String country, String area, String number, String inline)
format Phone No
StringBuffer sb = new StringBuffer();
boolean first = true;
if (country != null)
    country = country.trim();
if (area != null)
    area = area.trim();
if (number != null)
    number = number.trim();
...
StringformatPhoneNumber(String number)
format Phone Number
if (isEmpty(number))
    return "";
String phoneNumber = number.trim();
if (phoneNumber.length() == 11) {
    StringBuffer buf = new StringBuffer();
    buf.append(phoneNumber.substring(0, 1));
    buf.append("(");
    buf.append(phoneNumber.substring(1, 4));
...
StringformatPhoneNumber(String phoneNumber)
format Phone Number
try {
    String s = phoneNumber.replaceAll("\\(|\\)|:|-|\\.", "");
    ;
    s = s.replaceAll("(\\d{3})(\\d{3})(\\d{4})(\\d{3})*", "($1) $2-$3x$4");
    if (s.endsWith("x"))
        s = s.substring(0, s.length() - 1);
    return s;
} catch (Exception ex) {
...
StringformatPrintIntegerToString(long number, boolean printCommasP, boolean printSignP, int radix, int mincol, char padchar, char commachar, int commainterval)
format Print Integer To String
String text = Long.toString(Math.abs(number), radix);
String commaed = (printCommasP) ? formatAddCommas(text, commachar, commainterval) : text;
String signed = (number < 0) ? "-" + commaed : (printSignP) ? "+" + commaed : commaed;
return formatWriteFieldToString(signed, mincol, 1, 0, padchar, true);
voidformatRowKey(final long number, final int digits, byte[] dest)
format Row Key
long d = Math.abs(number);
for (int ii = digits - 1; ii >= 0; ii--) {
    dest[ii] = (byte) ((d % 10) + '0');
    d /= 10;
StringformatSafetyNumber(String digits)
format Safety Number
final int partCount = 12;
int partSize = digits.length() / partCount;
StringBuilder f = new StringBuilder(digits.length() + partCount);
for (int i = 0; i < partCount; i++) {
    f.append(digits, i * partSize, (i * partSize) + partSize).append(" ");
return f.toString();
StringformatSheetNumber100(final int sheetNumber)
format Sheet Number
if (sheetNumber < 10) {
    return "00" + sheetNumber;
} else if (sheetNumber < 100) {
    return "0" + sheetNumber;
} else {
    return String.valueOf(sheetNumber);
StringformatSmallerNumber(String prefix, String suffix, long amnt, boolean useDecimals)
format Smaller Number
switch (Long.toString(amnt).length()) {
case 4:
    prefix += Long.toString(amnt).substring(0, 1)
            + (useDecimals ? "." + Long.toString(amnt).substring(1, 3) : "") + "K" + suffix;
    return prefix;
case 5:
    prefix += Long.toString(amnt).substring(0, 2)
            + (useDecimals ? "." + Long.toString(amnt).substring(2, 4) : "") + "K" + suffix;
...
StringformatTo2Digit(int number)
Used to format number in 2 digits
return number < 10 ? "0" + number : Integer.toString(number);
StringNumberFormat(double str, int max, int min)
Number Format
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(max);
nf.setMinimumFractionDigits(min);
return nf.format(str);