Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Comparing two values in if condition

former_member798210
Discoverer
0 Likes
4,930

Hello everyone.


i have a scenario where i get a value into a variable which is type string at runtime and i have to write it is positive or negative or zero in the output.

Here is the code

Consider a variable GV_STRING type string.


if gv_string gt 0.
write ‘positive’.

Elseif gv_string lt 0.

Write ‘negative’.

Else.

Write ’zero’.

Endif.

If the value i am getting is between -0.4 to -0.1 and 0.1 to 0.4 it is writing as zero which is not the expected output.

And when i get a character it leads to run time error but the expected output is to write zero.

So i have done some experiments and i modified my code a little.

On the above code instead of giving zero simply, i gave them in single quotation as shown in below.

if gv_string gt ‘0’.
write ‘positive’.

Elseif gv_string lt ‘0’.

Write ‘negative’.

Else.

Write ’zero’.

Endif.


its output is as i expected but i don’t know why it is working when i gave them in single quotation and in the above code also when an character comes then it write as positive but i want when a character comes i need to write it as zero

1 ACCEPTED SOLUTION
Read only

matt
Active Contributor
4,232

In your first code, your string is converted to an integer because you're comparing with a number without quotes, which is always treated as an integer. In the second, because you're comparing with a number in quotes, the string is converted to a floating point number.

Hello everyone.


i have a scenario where i get a value into a variable which is type string at runtime and i have to write it is positive or negative or zero in the output.

Here is the code

Consider a variable GV_STRING type string.


if gv_string gt 0.
write ‘positive’.

Elseif gv_string lt 0.

Write ‘negative’.

Else.

Write ’zero’.

Endif.

If the value i am getting is between -0.4 to -0.1 and 0.1 to 0.4 it is writing as zero which is not the expected output.

And when i get a character it leads to run time error but the expected output is to write zero.

So i have done some experiments and i modified my code a little.

On the above code instead of giving zero simply, i gave them in single quotation as shown in below.

if gv_string gt ‘0’.
write ‘positive’.

Elseif gv_string lt ‘0’.

Write ‘negative’.

Else.

Write ’zero’.

Endif.


its output is as i expected but i don’t know why it is working when i gave them in single quotation and in the above code also when an character comes then it write as positive but i want when a character comes i need to write it as zero

6 REPLIES 6
Read only

venkateswaran_k
Active Contributor
4,232

Hi,

You are trying to compare the string variable to numeri value. It will not work. You need to do like this.

data gv_string type string.
data gv_numeric type p DECIMALS 2.
data gv_type type DD01V-DATATYPE.

gv_string = '-0.4'.
CALL FUNCTION 'NUMERIC_CHECK'
EXPORTING
string_in = gv_string
IMPORTING
* STRING_OUT =
HTYPE = gv_type.

if gv_type = 'NUMC'.
gv_numeric = gv_numeric.
if gv_numeric < 0.
write 'Negative'.
else.
write 'Positive'.
endif.
else.
write 'Not Numeric = Zero'.
endif.

Regards,

Venkat

Read only

venkateswaran_k
Active Contributor
4,232

Or you can use try catch and then do your if statement as below

TRY.
gv_numeric = gv_string.
CATCH CX_SY_CONVERSION_NO_NUMBER.
MESSAGE 'no number' type 'S' DISPLAY LIKE 'S'.
ENDTRY.

  if gv_numeric < 0.
    write 'Negative'.
  elseif gv_numeric > 0..
    write 'Positive'.
  else.
    write 'Zero - Not numeric'.
  endif.

Regards,

Read only

matt
Active Contributor
4,232

Please in future use the CODE button in the editor to display code. Like venkateswaran.k has.

Read only

matt
Active Contributor
4,233

In your first code, your string is converted to an integer because you're comparing with a number without quotes, which is always treated as an integer. In the second, because you're comparing with a number in quotes, the string is converted to a floating point number.

Read only

0 Likes
4,232

hai Matthew Billingham.

While i was providing input as GE 0.5 or 0.5-it is giving output as expected in the first code means upto my understanding it is rounding the value to a nearest whole number.


i am correct, if wrong please guide me.

Thank you.

Venkata kalyan

Read only

former_member808116
Participant
0 Likes
4,232

STEP 1. You can check input parameters is number.

STEP 2. Compare two value in If conditions