%DEC 用法总结(Convert to Packed Decimal Format)
时间:2016-03-02 ┊ 阅读:6,163 次 ┊ 标签: 开发 , 编程 , 经验
%DEC (Convert to Packed Decimal Format)
%DEC(numeric or character expression{:precision:decimal places})
%DEC(date time or timestamp expression {:format})
%DEC converts the value of the first parameter to decimal (packed) format.
Numeric or character expression
When the first parameter is a numeric or character expression, the result has precision digits and decimal places decimal positions. The precision and decimal places must be numeric literals, named constants that represent numeric literals, or built-in functions with a numeric value known at compile-time.
Note: %LEN and %DECPOS cannot be used directly for the second and third parameters of %DEC or %DECH, even if the values of %LEN and %DECPOS are constant. See Figure 206 on page 509 for an example using the length and decimal positions of a variable to control %DEC and %DECH.
Parameters precision and decimal places may be omitted if the type of expression is neither float nor character. If these parameters are omitted, the precision and decimal places are taken from the attributes of the numeric expression.
If the parameter is a character expression, the following rules apply:
v The sign is optional. It can be ’+’ or ’-’. It can precede or follow the numeric data.
v The decimal point is optional. It can be either a period or a comma.
v Blanks are allowed anywhere in the data. For example, ’ + 3 ’ is a valid parameter.
v The second and third parameters are required.
v Floating point data, for example ’1.2E6’, is not allowed.
v If invalid numeric data is found, an exception occurs with status code 105.
See %DECHfor examples using %DEC.
Date, time or timestamp expression
When the first parameter is a date time or timestamp expression, the optional format parameter specifies the format of the value returned. The converted decimal value will have the number of digits that a value of that format can have, and zero decimal positions. For example, if the first parameter is a date, and the format is *YMD, the decimal value will have six digits.
If the format parameter is omitted, the format of the first parameter is used. See “DATFMT(fmt{separator})” on page 252 and “TIMFMT(fmt{separator})” on page 262. Format *USA is not allowed with a time expression. If the first parameter is a time value with a time-format of *USA, the second format parameter for %DEC must be specified.
Figure 183 on page 476 shows an example of the %DEC built-in function. %DEC (Convert to Packed Decimal Format)
For more information, see “Conversion Operations” on page 410 or “Built-in Functions” on page 394.
D yyddd S 5S 0
D yyyymmdd S 8P 0
D hhmmss S 6P 0
D numeric S 20S 0
D date S
D inz(D’2003-06-27’) DATFMT(*USA)
D time S T inz(T’09.25.59’)
D timestamp S Z inz(Z’2003-06-27-09.25.59.123456’ /free
// Using the format of the first parameter
numeric = %dec(date); // numeric = 06272003
numeric = %dec(time); // numeric = 092559
numeric = %dec(timestamp); // numeric = 20030627092559123456
// Using the second parameter to specify the result format
yyddd = %dec(date : *jul); // yyddd = 03178
yyyymmdd = %dec(date : *iso); // yyyymmdd = 20030627
Figure 182. Using %DEC to convert dates, times and timestamps to numeric