Reputation: 25
I don't have much knowledge in RPGLE. I am trying to solve this small exercise given to me. I never worked or seen exercises using decimals. I want to get percentage of 7 values. So, I want to do the following calculation.
For example in RPGLE.
DTotal S 3P 0
DPercnt S 2P 2
/Free
Total = 589;
Percnt = (Total/700)100;
Dsply Percnt;
*Inlr = *On;
/End-Free
Expected output is 84.142857143
or round off decimal places to two (84.14)
or four (84.1430)
places.
Total
and Percent
Variables?Percent
variable (Packed or Zoned) to hold two or four or N decimal places?Errors I got and corrections I carried-out:
The end of the expression is expected.
(with in SEU)
For the above error, I added *
and it solved Percnt = (Total/700) * 100;
Below is the second error.
The target for a numeric operation is too small to hold the result (C G D F)
For above error, I increased Percnt
variable length to 4P 2
.
But this time answer was wrong, DSPLY 8414
. Not as expected.
So, I used Eval
as suggested below. Eval(H) Percnt = (Total/700) * 100;
Still same output: DSPLY 8414
.
Thanks @Barbara. I noticed this tip late. DSPLY (%CHAR(Percnt))
gave me the desired output 84.14
.
Upvotes: 2
Views: 2961
Reputation: 3674
RPG numeric variables are defined by the total number of digits and number of decimal places. So if you want two integer places and two decimal places (12.34), define the variable as packed(4:2) in free-form or 4P 2 in fixed form.
If you want rounding, code the (H) extender ('H' stands for "half-adjust") for the opcode. The opcode for assignments is the EVAL opcode, which is usually omitted. But you have to code it if you want to add an extender.
DTotal S 3P 0
DPercnt S 4P 2
Total = 589;
Eval(H) Percnt = (Total/700) * 100;
DSPLY (%CHAR(Percnt));
*Inlr = *On;
Unless you are on a release prior to 7.1, you don't need to code /FREE and /END-FREE any more.
Upvotes: 5