2022 Jul 13 6:34 AM
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
2022 Jul 13 9:40 AM
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
2022 Jul 13 6:48 AM
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
2022 Jul 13 7:03 AM
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,
2022 Jul 13 9:39 AM
Please in future use the CODE button in the editor to display code. Like venkateswaran.k has.
2022 Jul 13 9:40 AM
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.
2022 Jul 13 3:31 PM
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
2022 Jul 15 1:44 AM
STEP 1. You can check input parameters is number.
STEP 2. Compare two value in If conditions
| User | Count |
|---|---|
| 3 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |