Using IBM i? Need to create Excel, CSV, HTML, JSON, PDF, SPOOL reports? Learn more about the fastest and least expensive tool for the job: SQL iQuery.
When using %DEC, you are not allowed to specify %LEN(targetVar) and %DECPOS(targetVar) for the second and third parameters of the %DEC built-in function. The code below illustrates how to use named constants to circumvent this shortcoming.
D numVar S 7P 2 D charVar S 10A Inz(' 543.21') D NUMLEN C Const(%len(numVar)) D NUMPOS C Const(%decpos(numVar)) C EVAL *INLR = *ON /free numVar = %dec( charVar : 7 : 2); // Valid numVar = %dec( charVar : %Len(numVar) : %decPos(numVar)); // Fail numVar = %dec( charVar : NUMLEN : NUMPOS ); // Valid return; /end-free
As seen on line 14 above, the named constants NUMLEN and NUMPOS may be used when you want the target variable's attributes to influence the %DEC built-in function. Line 12 illustrates an assumed valid syntax, but does not compile. So the code on line 14 is necessary, or worse, the code on line 10 (hard coding the values) may be used.