%EDITC(使用编辑码编辑值)

%EDITC(numeric : editcode {: *ASTFILL | *CURSYM | currency-symbol})
此函数返回表示根据编辑码编辑的数字值的字符结果。 通常,数字值和编辑代码的规则与在输出规范中编辑数字值的规则相同。 第三个参数是可选的,如果指定了此参数,那么必须是下列其中一个参数:
*ASTFILL
指示要使用星号保护。 这意味着将返回值中的前导零替换为星号。 例如, %EDITC (-0012.5 : 'K ': *ASTFILL) 返回 "***12.5-"。
*CURSYM
指示要使用浮动货币符号。 实际符号将是在 CURSYM 关键字中的控制规范上指定的符号,或缺省值 "$"。 当指定 *CURSYM 时,货币符号将放在结果中第一个有效数字之前。 例如, %EDITC (0012.5 : 'K ': *CURSYM) 返回 ' $12.5 '
货币符号
指示浮动货币将与提供的货币符号配合使用。 它必须是 1 字节字符常量 (文字,命名常量或可在编译时求值的表达式)。 例如, %EDITC (0012.5 : 'K ': 'X') 返回 '   X12.5 '。

%EDITC 的结果始终是相同的长度,并且可能包含前导和尾部空格。 例如,对于 NUM 的一个值,%EDITC(NUM : 'A' : '$') 可能会返回 '$1,234.56CR' ,而对于另一个值,%EDITC(NUM : 'A' : '$') 可能会返回 ' $4.56'。

在第一个参数中不允许使用浮点表达式 (可以使用 %DEC 将浮点转换为可编辑格式)。 在第二个参数中,将编辑码指定为字符常量; 支持的编辑码为: "A"-"D" , "J"-"Q" , "X"-"Z" , "1"-"9"。 该常量可以是字面值,命名常量或可在编译时确定其值的表达式。

有关更多信息,请参阅 转换操作内置函数

图 1。 %EDITC 示例 1
DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++++++++++++++++++++++
D msg             S            100A
D salary          S              9P 2 INZ(1000)
 * If the value of salary is 1000, then the value of salary * 12
 * is 12000.00. The edited version of salary * 12 using the A edit
 * code with floating currency is ' $12,000.00 '.
 * The value of msg is 'The annual salary is $12,000.00'
CL0N01Factor1+++++++Opcode&ExtExtended-factor2+++++++++++++++++++++++++++
C                   EVAL      msg = 'The annual salary is '
C                                + %trim(%editc(salary * 12
C                                              :'A': *CURSYM))
 * In the next example, the value of msg is 'The annual salary is &12,000.00'
C                   EVAL      msg = 'The annual salary is '
C                                + %trim(%editc(salary * 12
C                                              :'A': '&'))

 * In the next example, the value of msg is 'Salary is $*****12,000.00'
 * Note that the '$' comes from the text, not from the edit code.
C                   EVAL      msg = 'Salary is $'
C                                + %trim(%editc(salary * 12
C                                               :'B': *ASTFILL))

 * In the next example, the value of msg is 'The date is 1/14/1999'
C                   EVAL      msg = 'The date is '
C                                + %trim(%editc(*date : 'Y'))
常见需求是编辑字段,如下所示:
  • 禁止前导零
  • 如果值为负数,那么会将括号放在该值周围

以下操作在子过程中使用 %EDITC 完成此操作:

图 2. %EDITC 示例 2
DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++++++++++++++++++++++
D neg             S              5P 2      inz(-12.3)
D pos             S              5P 2      inz(54.32)
D editparens      PR            50A
D    val                        30P 2      value
D editedVal       S             10A
CL0N01Factor1+++++++Opcode&ExtExtended-factor2+++++++++++++++++++++++++++
C                   EVAL      editedVal = editparens(neg)
 * Now editedVal has the value '(12.30)   '
C                   EVAL      editedVal = editparens(pos)
 * Now editedVal has the value ' 54.32    '
 *---------------------------------------------------------------
 * Subprocedure EDITPARENS
 *---------------------------------------------------------------
P editparens      B
D editparens      PI            50A
D    val                        30P 2      value
D lparen          S              1A        inz(' ')
D rparen          S              1A        inz(' ')
D res             S             50A
 * Use parentheses if the value is negative
C                   IF        val < 0
C                   EVAL      lparen = '('
C                   EVAL      rparen = ')'
C                   ENDIF
 * Return the edited value
 * Note that the '1' edit code does not include a sign so we
 * don't have to calculate the absolute value.
C                   RETURN    lparen             +
C                             %editc(val : '1')  +
C                             rparen
P editparens      E