Java Number Format formatPrintIntegerToString(long number, boolean printCommasP, boolean printSignP, int radix, int mincol, char padchar, char commachar, int commainterval)

Here you can find the source of formatPrintIntegerToString(long number, boolean printCommasP, boolean printSignP, int radix, int mincol, char padchar, char commachar, int commainterval)

Description

format Print Integer To String

License

Open Source License

Declaration

public static String formatPrintIntegerToString(long number, boolean printCommasP, boolean printSignP,
            int radix, int mincol, char padchar, char commachar, int commainterval) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static String formatPrintIntegerToString(long number, boolean printCommasP, boolean printSignP,
            int radix, int mincol, char padchar, char commachar, int commainterval) {
        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);
    }//from   www .j  a  v  a2 s  .com

    public static String formatAddCommas(String string, char commachar, int commainterval) {
        int length = string.length();
        int[] multipleResults = truncate(length - 1, commainterval);
        int commas = multipleResults[0];
        int extra = multipleResults[1];
        StringBuffer newString = new StringBuffer(length + commas);
        int firstComma = extra + 1;
        newString.setLength(length + commas);
        {
            String replacement = string.substring(0, firstComma);
            newString.replace(0, firstComma, replacement);
        }
        int src = firstComma;
        for (int dst = firstComma; src != length; src = src + commainterval, dst = dst + commainterval + 1) {
            newString.setCharAt(dst, commachar);
            String replacement = string.substring(src, src + commainterval);
            newString.replace(dst + 1, Math.min(newString.length(), 1 + dst + replacement.length()), replacement);
        }
        return newString.toString();
    }

    public static String formatWriteFieldToString(String string, int mincol, int colinc, int minpad, char padchar,
            boolean padleft) {
        StringBuffer str = new StringBuffer();
        minpad = Math.max(0, minpad);
        if (!padleft) {
            str.append(string);
        }
        for (int i = 0; i < minpad; ++i) {
            str.append(padchar);
        }
        for (int chars = string.length() + minpad; chars < mincol; chars = chars + colinc) {
            for (int i = 0; i < colinc; ++i) {
                str.append(padchar);
            }
        }
        if (padleft) {
            str.append(string);
        }
        return str.toString();
    }

    public static int[] truncate(int a, int b) {
        int quo = a / b;
        return new int[] { quo, a - (quo * b) };
    }

    public static long[] truncate(long a, long b) {
        long quo = a / b;
        return new long[] { quo, a - (quo * b) };
    }
}

Related

  1. formatPercentage(Number numerator, Number denominator)
  2. formatPhone(String phoneNumber, String formattingPattern, String countryCode)
  3. formatPhoneNo(String country, String area, String number, String inline)
  4. formatPhoneNumber(String number)
  5. formatPhoneNumber(String phoneNumber)
  6. formatRowKey(final long number, final int digits, byte[] dest)
  7. formatSafetyNumber(String digits)
  8. formatSheetNumber100(final int sheetNumber)
  9. formatSmallerNumber(String prefix, String suffix, long amnt, boolean useDecimals)