Armakatsu
Armakatsu

Reputation: 13

ECL Decimal Casting

I'd like to ask the reason why this is happening.

When I was casting a result to DECIMAL5_2 when using COUNT, it works as planned when the COUNT is in a separate line from the casting:

c1 = COUNT($.MenInMStatesPersons(IsBornBefore1980);
c2 = COUNT(MenBornBefore1980);
PercOlderMalesInMStates := (DECIMAL5_2)(c1/c2 * 100); //result 14.46

And when I try to condense it to a one-liner, the output changes:

PercOlderMalesInMStates := (DECIMAL5_2)(COUNT($.MenInMStatesPersons(IsBornBefore1980)/COUNT(MenBornBefore1980) * 100); 
//result is 15

But the one liner will work if I declare the value to be of the DECIMAL5_2 type:

DECIMAL5_2 PercOlderMalesInMStates := (COUNT($.MenInMStatesPersons(IsBornBefore1980)/COUNT(MenBornBefore1980) * 100); 
//result is 14.86

Thank you very much!


EDIT: There's a difference between the code I posted above and the one in the actual ECL file in my PC. Apparently, I typed a working expression here while I fumbled the bag in my local ECL file.

One-Liner in this post:

PercOlderMalesInMStates := (DECIMAL5_2)(COUNT($.MenInMStatesPersons(IsBornBefore1980)/COUNT(MenBornBefore1980) * 100); 
//result is indeed correct (14.86)

One-Liner in my local ECL file:

PercOlderMalesInMStates := (DECIMAL5_2) (COUNT($.MenInMStatesPersons(IsBornBefore1980)) / COUNT(MenBornBefore1980)) * 100;
//result is incorrect(15)

Notice the difference in the parentheses! My guess now as to why this is happening would be that in my local ECL file, the DECIMAL5_2 casting happens before the fraction part can be multiplied by 100, thus it becomes an INTEGER.

Thank you again and sorry for the mistake!

Upvotes: 1

Views: 66

Answers (1)

Richard Taylor
Richard Taylor

Reputation: 780

On my machine, this code produces exactly the same (correct) answer for all three:

IMPORT $;
IsBornBefore1980 := $.Persons.BirthDate < '1980'; 
MenBornBefore1980 := $.Persons(Gender = 'M',BirthDate < '1980'); 
c1 := COUNT($.MenInMStatesPersons(IsBornBefore1980));
c2 := COUNT(MenBornBefore1980);
PercOlderMalesInMStates1 := (DECIMAL5_2)(c1/c2 * 100); //result 14.46
PercOlderMalesInMStates2 := (DECIMAL5_2)(COUNT($.MenInMStatesPersons(IsBornBefore1980))/COUNT(MenBornBefore1980) * 100); 
DECIMAL5_2 PercOlderMalesInMStates3 := COUNT($.MenInMStatesPersons(IsBornBefore1980))/COUNT(MenBornBefore1980) * 100; 
PercOlderMalesInMStates1;
PercOlderMalesInMStates2;
PercOlderMalesInMStates3;

I'm not sure why your code produces different results, but I did have to correct several syntax errors before my version of your code would run. Try my version and see what you get.

HTH,

Richard

Upvotes: 1

Related Questions