cancel
Showing results for 
Search instead for 
Did you mean: 

User defined function to remove decimal places, right align with zero fill

Former Member
0 Kudos

Dear friend,

My source field (Quantity) is of data type QUAN with 3 decimal places. I want the target field as a whole number (no decimals), right adjusted and zero-filled.

Can anyone please send me the user defined function for the same.

Thanks in advance.

Jose Augastine

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Create a simple UDF that takes in one arguement. Use the code below in the UDF

double d = Double.parseDouble(a);

int i=(int) Math.round(d);

return Integer.toString(i);

This UDF will return a whole number. I do not get what you mean by right adjusted, zero filled. Can you give an example??

Regards,

Jai Shankar.

Former Member
0 Kudos

Right adjusted, zero filled means it has to be right aligned. and the remaining digits infront of it has to be filled with zeros.

example : The length of the field is 8.

Incoming value is 12.000

Output required is 00000012

(it should be right aligned and the remaining has to be filled with zeros)

Former Member
0 Kudos

Dear Jai Shankar,

I used ur UDF.

My input field was 12.000

I got the output as 12

But the required output is 00000012

(that is zeros has to be added infront of it bcos always its length has to be 😎

Shabarish_Nair
Active Contributor
0 Kudos

i am not tht good at java, neither i suppose this code is optimized since i wrote it ... but then you can use this as this works

public class ConvertDecimalToWhole {

public static void main(String[] args) {

String a = "3.14";

// double d = Double.parseDouble(a);

// int i=(int) Math.round(d);

// a = Integer.toString(i);

// System.out.println(a);

int n = 10;

String b[] = null;

a = a.replace('.',' ');

b = a.split(" ");

a = b[0].concat(b[1]);

System.out.println(a);

StringBuffer str = new StringBuffer(a);

int strLength = a.length();

if ( n > 0 && n > strLength ) {

for ( int j = 0; j <= n ; j ++ ) {

if ( j < n - strLength )

str.insert(0 , '0' );

}

}

System.out.println(str);

}

}

Do incoprporate the logic in your UDF !!1

ShaBZ~~

Note: Change the value of N to the maximum number of digits you have. In your case it is 8 i guess.

Message was edited by: Shabarish Vijayakumar

Former Member
0 Kudos

You can use the code given by Shabz or try this.

double d = Double.parseDouble(a);

DecimalFormat df=new DecimalFormat("00000000");

StringBuffer sb=new StringBuffer();

df.format(d,sb,new FieldPosition (NumberFormat.INTEGER_FIELD));

return(sb.toString());

Note: import java.text.*

Regards,

Jai Shankar.

Message was edited by: Jai Shankar

Former Member
0 Kudos

Thanks a lot Jai Shankar

Former Member
0 Kudos

Thanks a lot Shabarish

Shabarish_Nair
Active Contributor
0 Kudos

just one query ...

<i>double d = Double.parseDouble(a);

DecimalFormat df=new DecimalFormat("00000000");

StringBuffer sb=new StringBuffer();

df.format(d,sb,new FieldPosition (NumberFormat.INTEGER_FIELD));

return(sb.toString())</i> >>>>>

if i use it, suppose my input is 3.14 my output becomes 00000003. Will tht suffice ??? i guess u need 00000314

henrique_pinto
Active Contributor
0 Kudos

Dude,

just use standard FormatNumber function with Number Format parameter 00000000.

Regards,

Henrique.

Answers (0)