cancel
Showing results for 
Search instead for 
Did you mean: 

UDF for padding zeroes

Former Member
0 Kudos

Hi

I need a UDF Code for the following requirement ,which is used in various fields across a lot of mappings

Requirement is that if it is a fixed length file and the field length is 10 then whatever value thats coming should be right alligned and there shud be zeroes padded to the left of it

for e.g

field length is 10

input value os 3456

then the output should be 0000003456

I want to make it a generic function wher i can take the field length and the input value as inputs

Dev

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi,

If your input is always an numeric one then you can use formatnum as mentioned by sarvesh. Here if you get alphanumeric then this will throw exception. Then you need an udf like this:

Create a udf with one argument as input and add this code (here I am assuming your length of the output is 10 or change accordingly)::

int len=input.length();
 
for(int i=0; i<10-len;i++)
{
input="0"+input;
}
 
return input;

Here this will work for both numbers and alphabets.

Regards,

---Satish

Answers (2)

Answers (2)

justin_santhanam
Active Contributor
0 Kudos

Use the below code

Get two inputs as parameter ( one for length, second the value)

example: (10, 1234) or (14,900023)


int len = Integer.parseInt(a);
		StringBuffer sb = new StringBuffer();
		for(int i=b.length();i<len;i++)
		{
			sb.append("0");
		}
		sb.append(b);

return sb.toString();

Former Member
0 Kudos

No need of UDF, just use the std. function called FormatNum.

Double click the function and enter 10 zeros in first input box and do your mapping.

Former Member
0 Kudos

Hi Sarvesh

Why 10 zeroes ?

my requirement is

for a field length of 5 if the input is 42 the output should be 00042

similarly for a field length of 8 if the input is 876 then the value should be

00000876, if input is 9876 then it shud be 00009876

and so on ...

will FormatNum take care of right allignment also ?

Dev

Edited by: sd on Mar 26, 2010 4:44 PM

Former Member
0 Kudos

>

> Hi Sarvesh

>

> Why 10 zeroes ?

> my requirement is

>

> for a field length of 5 if the input is 42 the output should be 00042

>

> similarly for a field length of 8 if the input is 876 then the value should be

> 00000876, if input is 9876 then it shud be 00009876

>

> and so on ...

> will FormatNum take care of right allignment also ?

>

> Dev

>

> Edited by: sd on Mar 26, 2010 4:44 PM

Just try the function and your all queries will be answered.

Former Member
0 Kudos

Hi,

You can edit my function. What you can do is send the length as one input and then it should work. Add this code:

Create a value udf with two input arguments a and input. Then add this code:

Imports: java.*;

int len=input.length();
int b = Integer.parseInt(a);
 for(int i=0; i<b-len;i++)
{
input="0"+input;
}
 return input;

Here pass the total length of the field in first argument and then send the input value to teh second argument. Then it should work for you.

Regards,

---Satish