Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

Advanced udf to padding zeros.

Hi Experts,

Can you proved me the advanced udf to padding zeros. For example store is 41 as input, the output should be 0041.Even thought the standard function formatNum is available, my required is to create advanced udf. Can you please help out.

Thanks,

Chandar

Tags:
Former Member
Former Member replied

Hi Chandar,

Try this UDF.

Execution type is Single Values.

/**

     *

     * @param toPad

     *            the String to pad

     * @param padWith

     *            the String to pad toPad with

     * @param padToLength

     *            the desired length of the result

     * @return the padded String

     */

    public String padLeft(String toPad, String padWith, String padToLength, Container container) throws StreamTransformationException{

        int length = toPad.length();

        int padTo = Integer.parseInt(padToLength);

        if (length < padTo) {

            StringBuffer strB = new StringBuffer(length);

            int add = padTo - length;

            int iterations = add / padWith.length();

            for (int i = 0; i < iterations; i++)

                strB.append(padWith);

            // now to take care of any "remainder"

            int remainder = add % padWith.length();

            strB.append(padWith.substring(0, remainder));

            strB.append(toPad);

            return strB.toString();

        }

        return toPad;

    }

Regards,

Praveen.

0 View this answer in context

Helpful Answer

by
Not what you were looking for? View more on this topic or Ask a question