cancel
Showing results for 
Search instead for 
Did you mean: 

Float attributes addition gives me wrong result...

Former Member
0 Kudos

hi,

I am getting model node filled up through bapi...which is having amount as one of the field type char. In component controller , in one of the method i am loop through that node and summing up amount, by converting into float and at last i store that total amount to context attribute named TOTAL type float.

what my problem is , if i am having 3 rows in table having amount 1000, 2000 , & 3000.45 like this in char format so while looping i convert it into float and add this to temporary varible called total type float. and then i assign total variable to context attribute TOTAL_CONT type float.

for(int i=0;i<wdContext.nodeItem_det().size();i++)

{

total = total+Float.valueOf(wdContext.currentItem_detElement().getAmount()).floatValue();

}

wdContext.currentContextElement().setTotal_cont(total);

so first time total is having 1000.0 , second time it is having 3000.0 and third time its having 6000.45 but at last when it set up to context attribute Total_cont , and comes to screen it shows 6000.4503..

i dont understand from its having 03 at last.

please sort it out..

thanks

saurin shah

Accepted Solutions (0)

Answers (3)

Answers (3)

Former Member
0 Kudos

solved

nikhil_bose
Active Contributor
0 Kudos

what is 6000.4503..??? is same output you are getting?

the code posted seems okay. you can refer the modified code below; both will work


for(int i=0, total = 0; i<wdContext.nodeItem_det().size(); i++)
{
total = total+ (Float.valueOf( wdContext.nodeItem_det().getItem_detElementAt( i).getAmount())).floatValue();
}
wdContext.currentContextElement().setTotal_cont(total);

Next is to represent the attribute Total_cont

1. Create a Simple Type under Dictionaries

2. At Definition tab, select Built-in Type as float

3. go to Representation tab, specify External Representation Format as your desired format # stands for any number (example : ####.00)

4. change Total_cont attribute type to created simple type.

NikhiL

Former Member
0 Kudos

Hi Saurin,

Try printing out the value of Total just after the for loop and before you assign it to the context attribute.

If you get what you actually wanted, then check the properties of your context attribute.

i.e Try changing the type of your attribute to decimal/double/long and then check whether you get the desired result.

I guess it is printing that value you mentioned 6000.4503 as it is taking the exponentiation varaible e into consideration.

Regards

Kishan

Edited by: kishan chandranna on Dec 24, 2008 5:52 AM