Numeric Data Types
ABAP supports the numerical data types:
plus the internal types
s. The latter cannot
be specified directly in programs but are created when the predefined types INT1 or INT2 from ABAP Dictionary
are referenced. They are generally used in the same way as the type
i and are often converted to
The numeric data types are intended for calculations. Calculations involving fields of type
f correspond more or less directly to the operating system's machine commands
for the current application server. In contrast, calculations involving packed numbers of type
p are programmed in the ABAP runtime environment's kernel and are therefore somewhat slower.
Operations using the decimal floating point numbers
decfloat34 run using a library integrated into the ABAP kernel, until they are supported by the hardware of the application server.
The common generic type of the numeric data types is
- To a great extent,
decimal floating point numbers of the types
decfloat34replace the binary floating point numbers with type
f. See Notes on Use.
- The predefined type
n(numeric text field) is not a numeric number type, although its values are purely strings of digits; instead it is a character-like type, not generally to be used for calculations. Typical examples of numeric text fields are account numbers and article numbers, postal codes, and so on.
Intermediate results in arithmetic expressions of
i are stored in type
i auxiliary fields. Otherwise, type
i arithmetic is similar to performing calculations with type
In particular, division rounds numbers rather than truncating them and an overflow raises an exception.
i is typically used for counters, quantities, indexes, and offsets, as well as time periods.
p for packed numbers has a
value range that depends on their length and the number of
decimal places. Data objects of type
p can be 1 to 16 bytes long, with two
decimal places packed
into each byte, and one decimal digit and the sign packed into the last byte. There can be up to 14 decimal places. Packed numbers with decimal places cannot be specified directly in the program. Instead,
character literals must be used whose content can be interpreted as a packed number, that is, it represents a number in
Scientific notation is not permitted unless it can be interpreted as a mathematical notation.
Auxiliary fields for intermediate results in arithmetic expressions of calculation type p are always 16 bytes long and can thus hold up to 31 decimal places. Before an overflow, an arithmetic expression is calculated again with auxiliary fields that are twice as large or 63 decimal places. In the case of comparisons between packed numbers, the operand with fewer decimal places is also converted into an auxiliary field of this type, though an overflow occurs if the sum of the whole number places and decimal places exceeds 31.
If packed numbers are used, the
program attribute must always be set to fixed point arithmetic since
only this setting ensures that the decimal point is correctly calculated. Otherwise, all numbers are
specified as integers and all intermediate results are rounded up to the next integer. If fixed
point arithmetic is not configured, the decimal places defined for the number only appear in screen output or when formatting with
Calculations using calculation type
p are performed using
fixed point arithmetic.
In other words, a calculation is performed "commercially", similarly to using a pocket calculator or
paper and pencil. Type
p is typically used for values such as lengths, weights, and sums of money.
Decimal Floating Point Numbers
The data types fordecimal floating
point numbers are
The value range is 1E385(1E-16 - 1) through -1E-383, 0, +1E-383 through 1E385(1 - 1E-16) for
and 1E6145(1E-34 - 1) through -1E-6143, 0, +1E-6143 bis 1E6145(1 - 1E-34) for
The maximum precision is 16 decimal places or 34 decimal places, respectively. As well as its value, a decimal floating point number has a
scale and a
precision. These properties are not relevant for calculations and comparisons of values, but are used in
conversion rules and for formatting output.
Decimal floating point numbers with decimal places or exponents cannot be specified directly in the program. Instead, character literals must be used whose content can be interpreted as a packed number, that is, it represents a number in mathematical, scientific, or commercial notation.
Arithmetic expressions with decimal floating point numbers always have the
decfloat34. Each calculation is made with
decimal floating point arithmetic.
We recommend decimal floating point numbers if precision and a large range of values are of importance.
They do not have the disadvantages of binary floating point numbers described below. These binary floating
point numbers cannot represent each decimal number in their value range exactly. Decimal floating point numbers have a much large value range and a higher level of precision than packed numbers.
By using the
EXACT addition of the
it is possible to force a lossless calculation for decimal floating point numbers under certain circumstances. No rounding is permitted in lossless calculations and raises an exception.
Internally, decimal floating point numbers are represented by a 16-digit or 34-digit decimal mantissa and a decimal exponent. The exponent is between -383 and +384 or -6143 and + 6144, respectively. Apart from potential roundings in assignments and calculations, the effects discussed below for binary floating point numbers are not observed. This is because every 16-digit or 34-digit decimal number can be represented exactly.
Binary Floating Point Numbers
The data type for binary floating point numbers,
f, has a
value range of 2.2250738585072014E-308
through 1.7976931348623157E+308, positive as well as negative, and the number 0, with an accuracy of
at least 15 decimal places. In ABAP, 17 decimal places are represented. Whole numbers can be represented
exactly up to an absolute value of 2**53, which is equivalent to 9,007,199,254,740,992. Any larger numbers are rounded.
Binary floating point numbers cannot be specified directly in the program. Instead, character literals must be used whose content can be interpreted as floating point numbers, that is, it represents a number in scientific notation. Mathematical or commercial notation is not permitted unless it can be interpreted as scientific notation.
Internally, binary floating point numbers are stored separately, each in two parts. This can lead to
unexpected results despite the high degree of intrinsic accuracy. These occur mainly when performing conversions from and to type
- For example, the number 1.5 can be represented exactly in this notation since 1.5 = 1*2**0 + 1*2**(-1), but the number 0.15 can only be represented approximately by the number 0.14999999999999999. If 0.15 is rounded up to 1 valid digit, the result is 0.1 rather than 0.2 as might me expected. On the other hand, the number 1.5E-12 is represented by the number 1.5000000000000001E-12, which would be rounded up to 2E-12.
- A further real-life example: 7.27% of 73050 is to be calculated and rounded to 2 decimal places. The intermediate result is 5.3107349999999997E+03, since the correct result, 5310.735, cannot be represented exactly in two parts with 53 bits. (If the hardware cannot represent a real number exactly, it uses the next representable binary floating point number). After rounding, the result is 5310.73, rather than 5310.74 as might be expected.
The ABAP runtime environment always calculates commercially and not numerically like the underlying machine arithmetic. According to the rounding algorithm of the latter, the end digit 5 must always be rounded to the nearest even number (not the next largest number), that is, from 2.5 to 2 and from 3.5 to 4.
Note also that multiplication using powers of 10 (positive or negative) is not an exact operation.
- Example: Although it can be represented exactly in two parts, a binary floating point number
fof value 100.5, after the operation
f = f / 100 * 100.
has the value 100.49999999999999.
As well as rounding errors, the restricted number of decimal places for the mantissa can lead to the loss of trailing digits.
1 - 1.0000000000000001produces zero.
This means that the last digits in binary floating point arithmetic are not reliable. In particular,
it is not usually worth testing two binary floating point numbers
b for equality; instead, it is best to check whether the relative difference
abs((a - b)/a) is less than a predefined limit, such as 10**(-7).
Ultimately, the display and therefore the value of a binary floating point number stored in a database can be platform-dependent.
To assign numeric values to text fields and text strings, instead of using a
conversion it is often better to use the statement
WRITE ... TO or
embedded expressions in string templates with the associated formatting options.