ABAP中的基础数据类型(b, c, d, decfloat16, decfloat34, f, i, int8, n, p, s, string, t, x, xstring)

本文详细介绍了ABAP编程语言中的基础数据类型,包括数字类型、字符类类型、字节类型和日期时间类型,以及各自的数据范围、初始值和用法示例。此外,还讨论了数据字典中的内置数据类型,如INT1、INT2、INT4等,并列举了相关字符和金额类型的特殊数据类型。通过这篇博客,读者将深入理解ABAP中的数据类型及其应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

很多同学不太清楚ABAP语言中的基础数据类型(ABAP Type),本篇文章总结了,在ABAP语言中,预定义的数据类型以及其初始值和长度。

总的来说,在ABAP语言中,系统预定义了以下这些数据类型:

b, c, d, decfloat16, decfloat34, f, i, int8, n, p, s, string, t, x, xstring.

按照种类的不同,这些数据类型可以分为4大类:

  1. Numeric Types - 数字类型
  2. Character-like Types - 字符类类型
  3. Byte Types - 字节类型
  4. Date Types and Time Types - 时间和日期类型

1. Numeric types(数字类型)

用途:用于处理数字类型的数据

TypeLengthStandard LengthNamevalue rangeinitial value
b1 byte1-byte integer (internal)0 to 2550
s2 byte2-byte integer (internal)-32,768 to +32,7670
i4 byte4-byte integer-2,147,483,648 to +2,147,483,6470
int88 byte8-byte integer-9,223,372,036,854,775,808 to +9,223,372,036,854,775,8070
p1 to 16 bytes8 bytePacked number(-10^(2len-1) +1) / (10^(+dec)) to (+10^(2len-1) -1) /(10^(+dec))0
decfloat168 byteDecimal floating point number with 16 placesValid values are numbers between 1E385(1E-16 - 1) and -1E-383 for the negative range, 0 and +1E-383 to 1E385(1 - 1E-16) for the positive range.0
decfloat3416 byteDecimal floating point number with 34 placesValid values are numbers between 1E6145(1E-34 - 1) and -1E-6143 for the negative range, 0 and +1E-6143 and 1E6145(1 - 1E-34) for the positive range.0
f8 byteBinary floating point number with 17 placesValid values are numbers between -1.7976931348623157E+308 and -2.2250738585072014E-308 for the negative range and between +2.2250738585072014E-308 and +1.7976931348623157E+308 for the positive range, plus 0.0

用法举例:

DATA: num1   TYPE i,
      num2   TYPE i,
      result TYPE decfloat34.

...

result = num1 / num2.

2. Character-Like Types(字符类类型)

用途:用于处理字符和字符串类型的数据

TypeLengthDefault LengthNamevalue rangeinitial value
c1 to 262,143 charactersOne characterText fieldAny alphanumeric characters" " for every place
n1 to 262,143 charactersOne characterNumeric text fieldAny alphanumeric characters, but the only valid values are the digits 0 to 9“0” for every place
stringVariableText stringAs for type cEmpty string with length 0

用法举例:

DATA: flag TYPE c LENGTH 1,
      html TYPE string.

3. Byte-Like Types(字节类型)

用途:用于字节类型的数据

TypeLengthDefault LengthNamevalue rangeinitial value
x1 to 524,287 bytes1 byteByte fieldAny byte values, hexadecimal 00 to FFHexadecimal 00
xstringVariableByte stringAs for type xEmpty string with length 0

用法举例:

DATA hex TYPE xstring.
hex =  cl_abap_codepage=>convert_to( `...` ).

4. Date Types and Time Types(日期和时间类型)

用途:用于日期和时间类型的数据

TypeLengthDefault LengthNamevalue rangeinitial value
d8 charactersDate fieldt “yyyymmdd”: “yyyy” (year): 0001 to 9999, “mm” (month): 01 to 12, “dd” (day): 01 to 31“00000000”
t6 charactersTime field“hhmmss”. “hh” (hours): 00 to 23, “mm” (minutes): 00 to 59, “ss” (seconds): 00 to 59.“000000”

用法举例:

DATA: tomorrow  TYPE d,
      next_hour TYPE t.

DATA(today) = sy-datlo.
DATA(now)   = sy-timlo.

tomorrow  = today + 1.
next_hour = ( now + 3600 ) / 3600 * 3600.

5. 数据字典中的内置数据类型(Built-In Data Types)

ABAP中的基础数据类型,可以在程序中直接使用。

基于ABAP中的基本数据类型,SAP在数据字典SE11中提供了一系列的内置数据类型,我们可以使用这些内置的数据类型,来定义更加有业务含义的类型,这也便是domain的data element的概念了。

SAP在数据字典中的内置数据类型(built-in data type),是基于基础数据类型(abap type) 定义的。

在ABAP的数字字典中,提供了37种预制类型。下图展示了Domain ----- 预制数据类型-----基础数据类型建的关系。
在这里插入图片描述

数据字典中的37种预制数据类型,可分为以下几类。

5.1 Numeric Types相关的

TypeValid Places mDecimal Places nInitial ValueMeaningABAP Type
INT13-01-byte integer, 0 to 255b
INT25-02-byte integer, -32,768 to 32,767s
INT410-04-byte integer, -2,147,483,648 to +2,147,483,647i
INT819-08-byte integer, -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807int8
DEC1-310-m, maximum 140Packed number in BCD formatp, length m DIV 2 + 1, decimal places n
DF16_DEC1-150-m, maximum 140Decimal floating point number stored in BCD formatdecfloat16
DF16_RAW16-0Decimal floating point number stored in binary formatdecfloat16
DF34_DEC1-310-m, maximum 140Decimal floating point number stored in BCD formatdecfloat34
DF34_RAW34-0Decimal floating point number stored in binary formatdecfloat34
FLTP16160Binary floating point numberf

5.2 Character-Like Types相关的

TypeValid Places mInitial ValueMeaningABAP Type
CHAR1-30000, maximum of 1333 for table fieldsm blanksCharacter stringc, length m
LCHR256-32000Nonelong character stringc, length m
SSTRING1-1333Empty stringCharacter stringstring
STRING256-…Empty stringCharacter string (CLOB)string

5.3 Byte-Like Types相关的

TypeValid Places mInitial ValueMeaningABAP Type
RAW1-32000 maximum of 255 for table fieldsNoneByte stringx, length m
LRAW256-32000NoneLong byte stringx, length m
RAWSTRING256-…Empty stringByte string (BLOB)xstring

5.4 Date Types/Time Types相关的

TypeValid Places mInitial ValueMeaningABAP Type
DATS800000000Date in the format YYYYMMDDd
TIMS6000000Time in the format HHMMSSt
ACCP66 blanksPosting period in the format YYYYMMn, length 6

5.5 一些有特定含义的Character-Like Types:

TypeValid Places mInitial ValueMeaningABAP Type
NUMC1-255m zeroesNumeric textn, length m
CLNT3000Clientc, length 3
LANG1BlankLanguage keyc, length 1

5.6 金额、数量相关的类型(Currency Fields and Quantity Fields)

TypeValid Places mDecimal Places nInitial ValueMeaningABAP Type
CURR1-311-m, maximum 140Currency field in BCD formatp, length m DIV 2 + 1, decimal places n
CUKY5-5 blanksCurrency key for currency fieldsc, length 5
QUAN1-310-m, maximum 140Quantity field in BCD formatp, length m DIV 2 + 1, decimal places n
UNIT2-3-2 or 3 blanksUnit key of a quantity fieldc, length m

希望在你对ABAP数据类型有疑问的时候,本篇博客可以帮助到你 😃

本博客专注于技术分享,干货满满,持续更新。 欢迎关注❤️、点赞👍、转发📣!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

十年铸器

给作者赏杯咖啡

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值