Skip to Content
0

how to typecast float value to Integer in C4C?

Nov 30, 2016 at 05:20 AM

129

avatar image

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 ;

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

1 Answer

Horst Schaude
Nov 30, 2016 at 12:18 PM
0

Hello Sumit,

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

var num : IntegerValue;

HTH,
Horst

Show 5 Share
10 |10000 characters needed characters left characters exceeded

Hello Horst,

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

Cheers!

Thanks.

Regards,

Sumit.

0

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);

0

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

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!";

0

Hello Sumit,

You may follow the hints in Wikipedia.

HTH,
Horst

0