cancel
Showing results for 
Search instead for 
Did you mean: 

how to typecast float value to Integer in C4C?

0 Kudos

I am beginner in C4C development. I was trying to reverse a number. In Business object - I have created an element with data type Integer value (code snippet is below) and defined an action called as Reverse (Please refer Action Logic below) - every time when program enters into loop - the num value( num/10) is getting stored in float.

Kindly suggest how to convert this float value into interger.

*************LOGIC*******************

BO:

[Label("Enter Number:")] element num1:IntegerValue;

Action Reverse:

var num = this.num1; var revnum = 0;

while(num >= 1)

{

revnum = revnum * 10;

revnum = revnum + num%10 ;

num = (num/10);

} this.result = revnum ;

Accepted Solutions (0)

Answers (1)

Answers (1)

HorstSchaude
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hello Sumit,

Looks like the compiler is defining the "num" variabel as a float.
Please use this code:

var num : IntegerValue;

HTH,
Horst

0 Kudos

Hello Horst,

Now this is working fine. Thank you very much for your prompt response.

Cheers!

Thanks.

Regards,

Sumit.

0 Kudos

Hi Horst & All,

I have found one more issue while implementing the logic of "Armstrong number (which is nothing but - sum of the cubes of it's digits is equal to the number itself)

Example - 371 is an Armstrong number. But when it starts entering into loop - initially it works fine but in iteration 3 (please refer implemented logic below) when num = 37/10 = 3.7 = since the data type is IntegerValue and hence it should store as 3 (ideally, there should not be any round off), not 4. However this is getting round off and stored as 4. Hence, in turn result is showing as "Given number is not armstrong"

Kindly suggest on how to proceed on this. Thanks in advance.

******My Logic:************************

import ABSL;

import AP.Common.GDT as apCommonGDT;

var num:IntegerValue;

num = this.num1;

var temp = num;

var remainder;

while( num != 0)

{

remainder = num % 10;

this.result=this.result+(remainder*remainder*remainder);

num = num/10;

}

if(temp == this.result)

{

raise IsArmStrong.Create("E",temp);

}

else

raise NotArmstarong.Create("E",temp);

HorstSchaude
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hello Sumit,

We have "%" as the modulo operator but if you read the documentation in section 7.2.4.4 Arithmetic Expressions (Business Logic) you will not find the division mentioned.
Therefore you need to calculate the dividend in another way.

Sorry,
Horst

0 Kudos

Hi Horst,

Thanks for your response.

I have gone thru the section 7.2.4.4 and found that it supports division as well(please refer below). Here issue is, result is getting rounded off and hence this issue has happened.

Request you to kindly suggest if there is any other way to restrict the value from getting rounded off.

******************7.2.4.4 Arithmetic Expressions (Business Logic)**********************

Syntax literal | <varName> | <path expression> [ + | — | * | / | % ] <arithmetic expression>;

Description The arithmetic expressions support the common mathematical operators for addition, subtraction, multiplication, division, and modulo calculations. Operands can be atomic expressions, such as literals, variable identifiers, path expressions, or other arithmetic expressions. You can overwrite the precedence order of the operators by using parentheses. The operands have to evaluate to the exactly same type. The compiler does not implicitly convert incompatible types. The plus sign (+) operator is overloaded to allow string concatenation. Example var result = (-0.234e-5 + this.Total) * currentValue; var resultString = "Hello " + "world!";

HorstSchaude
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hello Sumit,

You may follow the hints in Wikipedia.

HTH,
Horst