on 09-28-2006 10:25 AM
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
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
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
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
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
User | Count |
---|---|
95 | |
11 | |
10 | |
9 | |
9 | |
7 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.