cancel
Showing results for 
Search instead for 
Did you mean: 

Take the decimal part of a number in mapping

former_member440449
Participant
0 Kudos

Hi,

If I receive a value as 123,456 and I need to map the integer number to FIELD1 and the decimal number to FIELD2 as this:

FIELD1=123

FIELD2=456

Can I do this without using UDFs?

Accepted Solutions (1)

Accepted Solutions (1)

anupam_ghosh2
Active Contributor
0 Kudos

Hi,

I don't think this is possible with standard text functions, as inputs to substring function are constants. You definitely need an UDF here.



String splitNumber(String s,int part,Container container)
	{
		String a[]=new String[2];
		a[0]=a[1]="";
		char decimalSeparetor=',';
		try
		{
			int i=0,j=0,l;
			for(l=s.length();i<l && s.charAt(i)!=decimalSeparetor;++i)
			{
				a[j]=a[j]+s.charAt(i);
			}
			for(j=1,i=i+1;i<l;++i)
			{
				a[j]=a[j]+s.charAt(i);
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
		return a[part];
	}

The UDF has two inputs as shown below


113,567 -------->splitNumber("113,567",0)    ---------->   113 (output)----->FIELD1
113,567--------->splitNumber("113,567",1)   ------------> 567 (output)------>FIELD2
113567---------->splitNumber("113567",0)----------------->113567 (output)-------->FIELD1
113567---------->splitNumber("113567",1)-----------------> no output ------>FIELD2

The value of integer part (to be supplied as constants) decides which whether you need the part of number before(0) the decimal separetor or after (1).

regards

Anupam

Answers (4)

Answers (4)

former_member440449
Participant
0 Kudos

Thanks a lot!!! All the UFDs worked!

naveen_chichili
Active Contributor
0 Kudos

Hi,

Yes .. you can use substring(standard function) which suits your requirement....

Regards,

Naveen

Former Member
0 Kudos

Hi

You can try using substring standard function

Regards

sandeep

Former Member
0 Kudos

Hi .

You can try this UDF.

You will declare two parameters one for value and the other for position (0 is for integer value ) (1 for decimal value)


   //write your code here
 String valSplit[] = Value.split(",");
 int position = Integer.parseInt(Pos);

 return valSplit[position].toString();