Java Number Format Pattern getFormattedQuantity(String qty, int len, String pad)

Here you can find the source of getFormattedQuantity(String qty, int len, String pad)

Description

Method getFormattedQuantity.

License

Open Source License

Parameter

Parameter Description
qty String
len int
pad String

Return

String

Declaration

public static String getFormattedQuantity(String qty, int len, String pad) 

Method Source Code

//package com.java2s;
/**//w w  w .  j  av a  2  s  .c  o m
 * @author David Garratt
 * 
 * Project Name : Commander4j
 * 
 * Filename     : JUtility.java
 * 
 * Package Name : com.commander4j.util
 * 
 * License      : GNU General Public License
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public 
 * License along with this program.  If not, see
 * http://www.commander4j.com/website/license.html.
 * 
 */

public class Main {
    /**
     * Method getFormattedQuantity.
     * 
     * @param qty
     *            String
     * @param len
     *            int
     * @param pad
     *            String
     * @return String
     */
    public static String getFormattedQuantity(String qty, int len, String pad) {
        String result = replaceNullObjectwithBlank(qty);

        char decimalseparator = java.text.DecimalFormatSymbols.getInstance().getDecimalSeparator();
        char thousandseparator = java.text.DecimalFormatSymbols.getInstance().getGroupingSeparator();

        if (result.equals("")) {
            result = "0";
        }

        result = result.trim();

        if (result.length() > 0) {
            String temp = "";

            Boolean intbit = false;
            if (result.indexOf(decimalseparator) < 0) {
                intbit = true;
            }

            for (int i = result.length() - 1; i >= 0; i--) {
                if ((result.charAt(i) == decimalseparator)) {
                    intbit = true;
                }

                if (intbit) {
                    if ((result.charAt(i) != decimalseparator) & (result.charAt(i) != thousandseparator))
                        temp = result.charAt(i) + temp;
                }
            }

            result = temp;
        }

        result = padString(result, false, len, pad);

        return result;
    }

    /**
     * Method replaceNullObjectwithBlank.
     * 
     * @param value
     *            Object
     * @return String
     */
    public static String replaceNullObjectwithBlank(Object value) {
        String result = "";

        if (value != null) {
            result = value.toString();
        }

        return result;
    }

    /**
     * Method padString.
     * 
     * @param size
     *            int
     * @param character
     *            String
     * @return String
     */
    public static String padString(int size, String character) {
        String s = "";

        for (int i = 0; i < size; i++) {
            s = s + character;
        }

        return s;
    }

    /**
     * Method padString.
     * 
     * @param input
     *            String
     * @param right
     *            boolean
     * @param size
     *            int
     * @param character
     *            String
     * @return String
     */
    public static String padString(String input, boolean right, int size, String character) {
        int inputlength = 0;
        String result = replaceNullStringwithBlank(input);

        inputlength = result.length();

        if (inputlength > size) {
            // result = result.substring(0,size-1);
            result = result.substring(0, size);
        } else {
            if (inputlength < size) {
                if (right == true) {
                    result = result + padString(size - inputlength, character);
                } else {
                    result = padString(size - inputlength, character) + result;
                }
            }
        }

        return result;
    }

    public static String replaceNullStringwithBlank(String value) {
        if (value == null) {
            value = "";
        }

        return value;
    }
}

Related

  1. getFormatedPrice(BigDecimal price)
  2. getFormattedInteger(int n)
  3. getFormattedNumber(int number)
  4. getFormattedNumber(NumberFormat numberFormat, String fieldValue, Class valueClass)
  5. getFormattedNumber(String input)
  6. getFormattedString4Digits(String number, String pattern)
  7. getFormatter()
  8. getFormatter()
  9. getFormatter(int precision)