Programming in RPG IV Third Edition 2 Objectives Perform arithmetic calculations in your programs Determine the appropriate size for numeric fields Determine how to round calculations to avoid truncation Convert data types Use numeric and character literals; and figurative constants
Programming in RPG IV Third Edition 3 Simple Numeric Assignment Assigning a value to a field simple means giving a field a value EVAL is used to assign values to fields –Evaluates the expression to the right of the equal sign –Stores value in the field to the left of the equal sign Coding EVAL is optional with Version 5 free-format * CL0N01Factor Opcode(E)+Extended-factor // Free-format (Version 5 or later) /FREE EVAL Result_field = expression; /END-FREE // Fixed-format (pre-Version 5) C EVAL Result_field = expression
Programming in RPG IV Third Edition 4 Using EVAL for Arithmetic Single expression can contain as many arithmetic operators, numeric literals, and numeric fields to accomplish a desired calculation Left of ‘=‘ is a numeric field, to the right can be numeric fields or numeric literals Rules of precedence are used to determine order of math operations
Programming in RPG IV Third Edition 5 Determining Field Sizes Arithmetic Operation Rule of ThumbExample AdditionResult should be one digit longer than longest factor SubtractionResult should be as long as longest factor MultiplicationResult field length should be the sum of the digit lengths of the factors 999 x DivisionResult field should be the sum of the digit length of the factors 999 /.1 = 9990
Programming in RPG IV Third Edition 6 Rounding (Half-Adjusting) To round, place an ‘H’ in the operation extender on the EVAL –Free-format specification requires explicit EVAL when using an extender (Version 5) Round only when necessary –The RPG compiler will issue a warning message about unnecessary half-adjusting –Unnecessary rounding reflects poorly on your programming skills and/or style * /FREE EVAL(H) Interest = LoanAmt * StdRate; EVAL(H) Interest = AltAmt * PrimeRate; /END-FREE
Programming in RPG IV Third Edition 7 Improving EVAL Precision To ensure that EVAL doesn’t truncate significant decimal places, use “Result Decimal Positions” option for expressions –Specify EXPROPT(*RESDECPOS) in H spec, to set default option for program –Or, specify (R) extender for a single EVAL statement Otherwise system defaults to EXPROPT(*MAXDIGITS) option * H EXPROPT(*RESDECPOS) /FREE EVAL(HR) Pay = HourlyRate * * HourlyRate *(HoursWorked -40); /END-FREE
Programming in RPG IV Third Edition 8 Sample Program - F Specs * // ********************************************************************** // This program calculates selling prices for received items based on a // 60% markup over item cost. It also determines projected gross profit // for each item in the shipment and total gross profit for all the items // ********************************************************************** FNewItems IF F 40 DISK FQPRINT O F 132 PRINTER
Programming in RPG IV Third Edition 9 Sample Program - D & I * // Definition of all work fields used in program D UnitProfit S 6 2 D SellPrice S 7 2 D GrssProfit S 10 2 D TotProfit S 14 2 // Input file of received items defined within the program INewItems NS I 1 30 Descript I ItemCost I QtyRcvd
Programming in RPG IV Third Edition 10 Sample Program - C Specs * /FREE EXCEPT Headings; READ NewItems; DOW NOT %EOF; EVAL(H) UnitProfit = ItemCost *.6; SellPrice = ItemCost + UnitProfit; GrssProfit = UnitProfit * QtyRcvd; TotProfit = GrssProfit + TotProfit; EXCEPT Detail; READ NewItems; ENDDO; EXCEPT TotalLine; *INLR = *ON; RETURN; /END-FREE
Programming in RPG IV Third Edition 11 Sample Program - O Specs * OQPRINT E Headings 1 O UDATE Y 11 O 55 'GROSS PROFIT PROJECTION' O 82 'PAGE' O PAGE 87 O E Headings 2 1 O 52 'SELLING' O 74 'PER UNIT' O 88 'TOTAL' O E Headings 2 O 10 'ITEM' O 39 'COST' O 51 'PRICE' O 61 'QTY' O 73 'PROFIT' O 92 'GROSS PROFIT’
Programming in RPG IV Third Edition 12 Sample Program - O Specs * O E Detail 1 O Descript 30 O ItemCost 1 41 O SellPrice 1 53 O QtyPcvd 1 62 O UnitProfit 1 74 O GrssProfit 1 92 O E TotalLine 1 O 70 'GRAND TOTAL GROSS PROFIT - O PROJECTED:' O TotProfit 1 92 '$'
Programming in RPG IV Third Edition 13 Character Assignment EVAL is used in character assignment –Both sides of assignment expression must be character fields or expressions Use a continuation character (+ or -) to continue a literal on the next line EVAL operation performs the assignment by transferring character by character, starting with the left-most character of the literal The result field is either padded with blanks or truncated May also use figurative constants –*BLANKS, *HIVAL, *LOVAL, etc.
Programming in RPG IV Third Edition 14 Data Type Conversion: MOVE MOVE operation transfers characters from Factor 2 field to the Result, character by character, moving through the fields from right to left –MOVEL moves from left to right Disregards decimal position in numeric fields To pad extra result positions with blanks, use (P) extender –Good practice: match the size of the Result and Factor 2 fields MOVE not supported by free-format * CL0N01Factor Opcode(E)+Factor Result Len++D+HiLoEq.... * Store a numeric value in character field Alpha (length 4) C MOVE 1234 Alpha * Store character field Alpha in a numeric field (length 4, 0 decimals) C MOVE Alpha Numeric
Programming in RPG IV Third Edition 15 Built in Functions (BIF) %ABS (Absolute Value) –Returns absolute value of expression, discarding sign %DIV (Divide) –Integer division %REM (Remainder) –Determines remainder from integer division (modulus) * /FREE TransTotal = %ABS(Debits - Credits); Hours = %DIV(Minutes:60); Minutes =%REM(Minutes:60); /END-FREE
Programming in RPG IV Third Edition 16 Points to Remember EVAL operation can express complex arithmetic calculations in a single free-form expression Truncation is based on the location of the decimal positions EVAL is used to assign values to character fields Operations MOVE and MOVEL are used to change data type –Not support by free-format Built-in functions perform complex calculations, string operations, and data-type conversion in free-format expressions