Java Size padString(String stringToPad, int size)

Here you can find the source of padString(String stringToPad, int size)

Description

Method "padString".

License

Open Source License

Parameter

Parameter Description
stringToPad the string to which white spaces must be added.
size the size of the new string.

Return

the given string completed with white spaces up the the given size.

Declaration

public static String padString(String stringToPad, int size) 

Method Source Code


//package com.java2s;
// %InstallDIR%\features\org.talend.rcp.branding.%PRODUCTNAME%\%PRODUCTNAME%license.txt

import java.math.BigDecimal;
import java.text.DecimalFormat;

import java.util.Locale;

public class Main {
    public static final int PERCENT = 0;
    public static final int INT_NUMBER = 1;
    public static final int DOUBLE_NUMBER = 2;

    /**//from  w  w  w  . j a  v a 2  s.com
     * Method "padString".
     * 
     * @param stringToPad the string to which white spaces must be added.
     * @param size the size of the new string.
     * @return the given string completed with white spaces up the the given size.
     */
    public static String padString(String stringToPad, int size) {
        return String.format("%" + size + "s", stringToPad); //$NON-NLS-1$ //$NON-NLS-2$
    }

    /**
     * DOC Zqin Comment method "format".
     * 
     * @param input the object that was formated.
     * @param style the style of formated, it should be 0, 1,2,99999.
     * @return the formated object.
     */
    public static Object format(Object input, int style) {

        try {
            if (checkInput(input)) {
                DecimalFormat format = null;

                // MOD qiongli 2011-6-1 bug 21589,if input is out of the decimal precision,replace it with threshold.
                BigDecimal zero = new BigDecimal(0);
                BigDecimal temp = new BigDecimal(input.toString());
                BigDecimal min = new BigDecimal(10E-5);
                BigDecimal max = new BigDecimal(9999 * 10E-5);
                boolean isUseScientific = false;
                switch (style) {
                case PERCENT:
                    if (temp.compareTo(min) == -1 && temp.compareTo(zero) == 1) {
                        isUseScientific = true;
                    } else if (temp.compareTo(max) == 1 && temp.compareTo(new BigDecimal(1)) == -1) {
                        input = max.toString();
                    }
                    format = (DecimalFormat) DecimalFormat.getPercentInstance(Locale.ENGLISH);
                    format.applyPattern("0.00%"); //$NON-NLS-1$
                    break;
                case INT_NUMBER:
                    min = new BigDecimal(10E-3);
                    if (temp.compareTo(min) == -1 && temp.compareTo(zero) == 1) {
                        isUseScientific = true;
                    }
                    format = (DecimalFormat) DecimalFormat.getNumberInstance(Locale.ENGLISH);
                    format.applyPattern("0"); //$NON-NLS-1$
                    break;
                case DOUBLE_NUMBER:
                    min = new BigDecimal(10E-3);
                    if (temp.compareTo(min) == -1 && temp.compareTo(zero) == 1) {
                        isUseScientific = true;
                    }
                    format = (DecimalFormat) DecimalFormat.getNumberInstance(Locale.ENGLISH);
                    format.applyPattern("0.00"); //$NON-NLS-1$
                    break;
                default:
                    format = (DecimalFormat) DecimalFormat.getInstance(Locale.getDefault());
                    return format.parse(input.toString());
                }
                if (isUseScientific) {
                    format.applyPattern("0.###E0%"); //$NON-NLS-1$
                }

                return format.format(new Double(input.toString()));
            } else {
                return input;
            }
        } catch (Exception e) {
            return input;
        }
    }

    /**
     * DOC Zqin Comment method "checkInput".
     * 
     * @param input the object that was formated.
     * @return true if the input is valid, else false;
     */
    private static boolean checkInput(Object input) {
        if (input == null || "".equals(input)) { //$NON-NLS-1$
            return false;
        } else {
            Double db = new Double(input.toString());
            if (db.equals(Double.NaN)) {
                return false;
            }
        }

        return true;
    }
}

Related

  1. getSizeString(long number, Locale locale)
  2. getSizeString(long size)
  3. getSizeText(long size)
  4. getSpaceSizeFromByte(long xB)
  5. getStringSize(String string)
  6. read(byte[] input, int size)
  7. readableBytes(long byteSize)
  8. readableSize(long num)
  9. readableSize(long size)