cancel
Showing results for 
Search instead for 
Did you mean: 

Error in mapping for floating Number calculation

Former Member
0 Kudos

Hi All,

I have a small doubt in floating number calculation in Mapping.

Actually i am geting a floating point number and calculating the SUM and generating the output. The input is of 2 decimal places(Ex: 26.02 and 26.03 ), but when it is adding all the values it is generating a three digit decimal number (Ex: 52.050003)

I dont know from where it is geting one extra number "2" in the output.

Please find the code for the same and let me know if i need to do something else to get ride of this.

**********************************************************************

//write your code here

float sum=0;

if(a != null && a.length > 0.00)

{

for ( int j =0; j<a.length;j++)

{

sum = sum + Float.parseFloat(a[j]);

}

result.addValue(String.valueOf(sum));

}

else

result.addValue("0");

***********************************************************************

Thanks in Advance,

JAY

Accepted Solutions (1)

Accepted Solutions (1)

justin_santhanam
Active Contributor
0 Kudos

Jay,

Please use the below code and let us know, if it helps.


BigDecimal sum= new BigDecimal("0");
BigDecimal bd;
if(a != null && a.length > 0.00)
{
for ( int j =0; j<a.length;j++)
{
bd=new BigDecimal(a[j]);
sum=sum.add(bd);
}
result.addValue(""+sum+"");
}
else
result.addValue("0");

in import section - java.math.*;

raj.

Edited by: Raj on Feb 18, 2008 11:11 AM

Answers (7)

Answers (7)

Former Member
0 Kudos

Raj U r a STAR...

It really solve my problem, and i just changed the code a little and passed all the value if it is of two dight after decimal and put that one inside the loop to get all the value.

if(count==0)

result.addValue(a<i>);

else

throw new RuntimeException("Values precisioned by more than 2 digits");

Once more thing can you please let me know from where i can find the java stuff that is required to generate the UDF in XI.

Warm Regards,

JAY

justin_santhanam
Active Contributor
0 Kudos

Jay,

>Once more thing can you please let me know from where i can find the java stuff that is required to generate the UDF in XI.

I would say basic core java is more than enough to work with these UDF's. You can see some of my collections in -http://www.flickr.com/photos/8764045@N06

raj.

Former Member
0 Kudos

I want to supress the whole message with generating the target.

Regards,

JAY

justin_santhanam
Active Contributor
0 Kudos

Jay,

Write this code and map the output to the root node of the target, let me know if it helps!


StringTokenizer st;
String temp;
int count=0;
for(int i=0;i<a.length;i++)
{
st= new StringTokenizer(a<i>,".");
st.nextToken();
temp=String.valueOf(st.nextToken());
if(temp.length()>2)
count+=1;
}
if(count==0)
result.addValue("");
else
throw new RuntimeException("Values precisioned by more than 2 digits");

raj.

Former Member
0 Kudos

Hi Patrick,

You are right,

Once we are geting 12.12 it should generate the output as 12.12

But if we are geting 12.123 then the souput should be SUPRESSED.

Hope you got it.

Regards,

JAY.

Former Member
0 Kudos

Hello,

maybe you could try something likes this:

if (sourceValue > FormatNumber(sourceValue))

return Supress;

else

return sourceValue;

[in the FormatNumber you have to use something like #.00 to get a value with two digits behind the separator]

Regards

Patrick

former_member859847
Active Contributor
0 Kudos

Hi Jay,

I hope with Format Number(arithimatic) you can display up to second decimal provided the properties of function should be 00.00.

regards

mahesh.

justin_santhanam
Active Contributor
0 Kudos

Jay,

From your above code, I understood that you will have multiple values coming from the source,that's the reason you used Queue.Could you let us know, if you get the following data, what needs to be done.

for example,

<value>12.12</value>

<value>14.12</value>

<value>11.1234</value>

<value>13.12</value>

So you don't want only 11.1234 or u must suppress the whole mapping by not generating the target.

raj.

Former Member
0 Kudos

I need to supress the whole file, not the single document.

So in the output i should get as SUPRESS,

As a result the file should go into error and we should convey this to the business.

Regards,

JAY

Former Member
0 Kudos

Hello,

i am a little bit confused that you talking now about files and documents.

But anyway if i get you right:

In the case you getting for example an input of 42.123

You want in your target a supress and not a 42.12

Is this right?

Regards

Patrick

Former Member
0 Kudos

Hi All/Raj,

I have to check the in-coming amount upto 2 decimal places only, if the incoming amount is more than 2 decimal places then it should get supressed.

Any Idea from ur side will of great help.

Regards,

JAY

Former Member
0 Kudos

Hello,

if you have more than 2 decimal places should the whole value supressed or only the values behind the second decimal place.

In the second case you can use the 'FormatNumber'-function.

Regards

Patrick

ranjit_deshmukh
Active Participant
0 Kudos

One question:

why are u using udf when you can do it simply by graphical Arithmatic functions?

Ranjit

Former Member
0 Kudos

Thanks a lot buddy..

This really solve my problem..

justin_santhanam
Active Contributor
0 Kudos

You welcome buddy!. Glad to know it helped.

raj.