'How to declare Decimal place in RPGLE Fixed or in free format?
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.
- Which Data type I should use for
TotalandPercentVariables? - How can I declare
Percentvariable (Packed or Zoned) to hold two or four or N decimal places? - Can anyone please type the declaration part and the calculation part in "RPG IV" or in Fixed-format please?
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 solvedPercnt = (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
Percntvariable length to4P 2.But this time answer was wrong,
DSPLY 8414. Not as expected.So, I used
Evalas 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 output84.14.
Solution 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | user3025253 |
