cancel
Showing results for 
Search instead for 
Did you mean: 

UDF to sum all values (with decimals) in a column of the input file

Former Member
0 Kudos

Hi All,

could any1 plz send me the udf that will calculate the sum of a column in the incoming file. plz do not give any reference to blogs/forum. I have gone through all. we cannot go for the SP 18 upgrade now. so looking for a workaround till then as the standard function --SUM has a bug summing all values with decimals.

i have checked the bolg forum below also, got few hints but did not help.

/people/thorsten.nordholmsbirk/blog/2006/04/03/never-ever-use-xis-built-in-arithmetic-functions

thanks.

rajib

Accepted Solutions (1)

Accepted Solutions (1)

baskar_gopalakrishnan2
Active Contributor
0 Kudos

Please do the following. Here is the UDF. This will count all the values of context.

public void sumValue(ResultList result, String[] value, Container container) throws StreamTransformationException{

double sum = 0.0;

for(int count =0; count<value.length; count++){

if(value[count]!=null && value[count].length() > 0){

sum = sum + new Double(value[count]).doubleValue();

}

}

BigDecimal bd1 = new BigDecimal(sum);

BigDecimal bd2 = bd1.setScale(2, BigDecimal.ROUND_HALF_DOWN);

result.addValue(bd2.toString());

}

Dont forget to add import statements

java.math.*

java.lang.*

This UDF takes care even if the input file misses value on the input side ....

Note: use square bracket before and after [count] in if block. This editor deleting it.

Baskar

Former Member
0 Kudos

thnz a lot Baskar..it is done

Answers (0)