Java String Normalize normalizeCutter(String cutter, int numDigits)

Here you can find the source of normalizeCutter(String cutter, int numDigits)

Description

normalize the cutter string for shelf list sorting - make number into decimal of the number of digits indicated by param

License

Open Source License

Declaration

private static String normalizeCutter(String cutter, int numDigits) 

Method Source Code

//package com.java2s;
import java.text.DecimalFormat;

public class Main {
    /**/* w ww.  ja v a2s  .co m*/
     * normalize the cutter string for shelf list sorting - make number into
     * decimal of the number of digits indicated by param
     */
    private static String normalizeCutter(String cutter, int numDigits) {
        String result = null;
        if (cutter != null && cutter.length() > 0) {
            String cutLets = getLCstartLetters(cutter);
            String cutDigs = cutter.substring(cutLets.length());
            String norm = null;
            if (cutDigs != null && cutDigs.length() > 0) {
                try {
                    // make sure part after letters is an integer
                    Integer.parseInt(cutDigs);
                    norm = normalizeFloat("." + cutDigs, 1, numDigits);
                } catch (NumberFormatException e) {
                    norm = cutDigs;
                }
            } else if (cutDigs.length() == 0 && cutLets.length() == 1)
                // if no digits in cutter, want it to sort first
                norm = normalizeFloat("0", 1, numDigits);

            result = cutLets + norm;
        }
        return result;
    }

    /**
     * Given a raw LC call number, return the initial letters (before any
     * numbers)
     */
    public static String getLCstartLetters(String rawLCcallnum) {
        String result = null;
        if (rawLCcallnum != null && rawLCcallnum.length() > 0) {
            String[] lcClass = rawLCcallnum.split("[^A-Z]+");
            if (lcClass.length > 0)
                result = lcClass[0];
        }
        return result;
    }

    /**
     * normalizes numbers (can have decimal portion) to (digitsB4) before
     * the decimal (adding leading zeroes as necessary) and (digitsAfter
     * after the decimal.  In the case of a whole number, there will be no
     * decimal point.
     *
     * @param floatStr,   the number, as a String
     * @param digitsB4    - the number of characters the result should have before the
     *                    decimal point (leading zeroes will be added as necessary). A negative
     *                    number means leave whatever digits encountered as is; don't pad with leading zeroes.
     * @param digitsAfter - the number of characters the result should have after
     *                    the decimal point.  A negative number means leave whatever fraction
     *                    encountered as is; don't pad with trailing zeroes (trailing zeroes in
     *                    this case will be removed)
     * @throws NumberFormatException if string can't be parsed as a number
     */
    public static String normalizeFloat(String floatStr, int digitsB4,
            int digitsAfter) {
        double value = Double.valueOf(floatStr).doubleValue();

        String formatStr = getFormatString(digitsB4) + '.'
                + getFormatString(digitsAfter);

        DecimalFormat normFormat = new DecimalFormat(formatStr);
        String norm = normFormat.format(value);
        if (norm.endsWith("."))
            norm = norm.substring(0, norm.length() - 1);
        return norm;
    }

    /**
     * return a format string corresponding to the number of digits specified
     *
     * @param numDigits - the number of characters the result should have (to be padded
     *                  with zeroes as necessary). A negative number means leave whatever digits
     *                  encountered as is; don't pad with zeroes -- up to 12 characters.
     */
    private static String getFormatString(int numDigits) {
        StringBuilder b4 = new StringBuilder();
        if (numDigits < 0)
            b4.append("############");
        else if (numDigits > 0) {
            for (int i = 0; i < numDigits; i++) {
                b4.append('0');
            }
        }
        return b4.toString();
    }
}

Related

  1. normalize(String text)
  2. normalize(String text)
  3. normalize(String uri)
  4. normalize(String value)
  5. normalize(String value, Locale locale)
  6. normalizeDose(String dose)
  7. normalizeEnglishIdentifier(String id)
  8. normalizeFieldNameOrPath(final String nameOrPath)
  9. normalizeIndex(String input, String[] indexList)