Rules for converting character values to numeric values using built-in functions

The following rules apply when the first parameter of %DEC, %DECH, %FLOAT, %INT, %INTH, %UNS or %UNSH is a character expression.

The rules also apply when data is processed for numeric fields by DATA-INTO and XML-INTO. In the following discussion, the rules related to %FLOAT apply to float fields.

  • If invalid numeric data is found, an exception occurs with status code 105.
  • The sign is optional. It can be '+' or '-'. For %FLOAT, it must precede the numeric data. For the other built-in functions, it can precede or follow the numeric data.
  • The decimal point is optional.

    If Control keyword EXPROPTS(*USEDECEDIT) is not specified, the decimal point can be either a period or a comma.

    If Control keyword EXPROPTS(*USEDECEDIT) is specified, the decimal point must be the character specified by the DECEDIT Control keyword.

  • Blanks are allowed anywhere in the data. For example, ' + 3 ' is a valid parameter. However, the data cannot be entirely blank unless Control keyword EXPROPTS(*ALWBLANKNUM) is specified.
  • The second and third parameters are required.
  • Floating point data, for example '1.2E6', is only allowed for %FLOAT.
  • For %FLOAT, the exponent is optional. It can be either 'E' or 'e'. The sign for the exponent is optional. It must precede the numeric part of the exponent.
  • Digit separators (such as thousands separators) are allowed if Control keyword EXPROPTS(*USEDECEDIT) is specified. The digit-separator character depends on the DECEDIT Control keyword.

    By default, and with DECEDIT('.') or DECEDIT('0.'), or with DECEDIT(*JOBRUN) when the job DECFMT value is not J, the period is the decimal-point character and the comma is the digit-separator character.

    With DECEDIT(',') or DECEDIT('0,'), or with DECEDIT(*JOBRUN) when the job DECFMT value is J, the comma is the decimal-point character and the period is the digit-separator character.

    For example, if DECEDIT(',') is specified, the decimal-point character is a comma and the separator character is a period. If EXPROPTS(*USEDECEDIT) is also specified, %DEC('1.234.567,89') returns 1234567.89.

    The following rules apply to the digit-separator character:
    • Digit separators are optional.
    • The digit-separator character must be preceded and followed by a numeric digit.
    • The digit-separator character can appear before and after the decimal point.
    • For example, if the decimal-point character is a period and the digit-separator character is a comma:
      • '1.2', '1,2.3', '1.2,3' are valid. The digit-separator character separates two digits in each case.
      • ' 1 . 2', '1 , 2 . 3', '1 . 2 , 3' are also valid. Blanks are ignored.
      • ',1' is not valid. The digit-separator character is not preceded by a digit.
      • '1,' is not valid. The digit-separator character is not followed by a digit.
      • '1.,2' is not valid. The digit-separator character is not preceded by a digit.
      • '1,.2' is not valid. The digit-separator character is not followed by a digit.

Examples

In the following example, keyword EXPROPTS(*USEDECEDIT) is not specified. The period (.) and comma (,) characters are both considered to represent the decimal point.
  1. Keyword EXPROPTS(*USEDECEDIT) is not specified.
  2. Parameter '1.2' is interpreted as 1.2.
  3. Parameter '1,2' is also interpreted as 1.2.

CTL-OPT;               //  1 

DCL-S num PACKED(5:2);

num = %DEC('1.2');     // = 1.2  2 
num = %DEC('1,2');     // = 1.2  3 
In the following example, keyword EXPROPTS(*USEDECEDIT) is specified. The DECEDIT keyword is not specified, but it defaults to DECEDIT('.'). The period (.) is considered to represent the decimal point and the comma (,) is considered to represent the digit separator.
  1. Keyword EXPROPTS(*USEDECEDIT) is specified.
  2. Parameter '1.2' is interpreted as 1.2.
  3. Parameter '1,2' is interpreted as 12. The comma (,) is ignored because it is the digit separator.

CTL-OPT EXPROPTS(*USEDECEDIT); //  1 

DCL-S num PACKED(5:2);

num = %DEC('1.2');     // = 1.2  2 
num = %DEC('1,2');     // = 12   3 
In the following example, keywords EXPROPTS(*USEDECEDIT) and DECEDIT(',') are specified. The comma (,) is considered to represent the decimal point and the period (.) is considered to represent the digit separator.
  1. Keyword EXPROPTS(*USEDECEDIT) and DECEDIT(',') are specified.
  2. Parameter '1.2' is interpreted as 12. The period (.) is ignored because it is the digit separator.
  3. Parameter '1,2' is interpreted as 1.2.

CTL-OPT EXPROPTS(*USEDECEDIT) DECEDIT(','); //  1 

DCL-S num PACKED(5:2);

num = %DEC('1.2');     // = 12   2 
num = %DEC('1,2');     // = 1.2  3