ABAP学习(23):WRITE语句

ABAP WRITE语句

ABAP使用Write,Write TO语句。

示例:

"******************************************************************
"Write使用
"******************************************************************
FORM f_test_write.
  DATA testTxt TYPE String value 'hello world!'.
  WRITE at / testTxt."换行
  WRITE at /5 testTxt."从第5列开始输出
  WRITE at /(10) testTxt."只显示10位字符
  WRITE at /5(15) testTxt."从第5列开始显示15位字符
  WRITE:/'****************************'.

  "write <obj> <format> 按照固定格式显示
  WRITE / testTxt LEFT-JUSTIFIED. "靠左显示
  WRITE / testTxt RIGHT-JUSTIFIED. "靠右显示
  WRITE / testTxt CENTERED. "居中显示
  WRITE / testTxt NO-GAP. "无空格显示

  DATA testTime type String value '1234311111'.
  WRITE / testTime USING EDIT MASK '__:__:__'. "使用固定格式显示
  Data testData type String value '00000134'.
  WRITE / testData no-ZERO."不输出前面0, no-sign 不输出正负号
  DATA testData1 type F value '3.44444'.
  WRITE / testData1 DECIMALS 3 . "显示小数点后位数
  DATA testData2 type F value '3.44564'.
  WRITE / testData2 ROUND 3. "四舍五入小数位数

  "write as checkbox
  WRITE:/'****************************'.
  DATA:  FLAG1  VALUE  '',
         FLAG2  VALUE  'X'.
  WRITE: / 'CHECK FLAG 1:' , FLAG1  AS  CHECKBOX.
  WRITE: / 'CHECK FLAG 2:' , FLAG2  AS  CHECKBOX.

  "write as sinbol Icon
  INCLUDE  <SYMBOL>.
  INCLUDE  <ICON>.
  WRITE: / 'Phone Symbol:' ,  SYM_PHONE  AS   SYMBOL.
  WRITE: / 'Alarm Icon:' ,  ICON_ALARM  AS  ICON.


  "write to
  "语法:WRITE {source|(source_name)} TO destination [format_options].
  "format_options:
*    [LEFT-JUSTIFIED|CENTERED|RIGHT-JUSTIFIED]
*    { { [EXPONENT exp]
*        [NO-GROUPING]
*        [NO-SIGN]
*        [NO-ZERO]
*        [CURRENCY cur]
*        { { [DECIMALS dec]
*            [ROUND scale] }
*          | [UNIT unit] } }
*      | { [ENVIRONMENT TIME FORMAT]
*          [TIME ZONE tz] }
*      [STYLE stl] }
*    [USING { {NO EDIT MASK}|{EDIT MASK mask} }]
*    [ DD/MM/YY   | MM/DD/YY
*    | DD/MM/YYYY | MM/DD/YYYY
*    | DDMMYY     | MMDDYY
*    | YYMMDD ]

  "示例1:转换xx,xxx
  DATA: testString TYPE string.
  DATA: testFloat TYPE P LENGTH 16 DECIMALS 2.
  DATA: testChar(20) TYPE C.
  testString = '-113233.00'.
  testFloat = testString.
  "转换为xxx,xxx表示
  WRITE testFloat TO testChar.
  WRITE:/ testChar.
  "去除前置空格
  CONDENSE testChar NO-GAPS.
  "添加负号
  testString = '-' && testChar.
  WRITE:/ testString.

  "示例2:
  DATA:date_long  TYPE c LENGTH 10.
  DATA:date_short TYPE c LENGTH 8.
  DATA:date_mask  TYPE c LENGTH 8.
  WRITE sy-datlo TO: date_short,
                     date_long,
                     date_mask DD/MM/YY.
  WRITE:/ date_short,date_long,date_mask.

  "示例3:字符串位置left,center,right
  DATA formatted_text TYPE c LENGTH 50.
  WRITE 'Left'   TO formatted_text LEFT-JUSTIFIED.
  "MESSAGE formatted_text TYPE 'I'.
  WRITE:/ formatted_text.
  WRITE 'Center' TO formatted_text CENTERED.
  "MESSAGE formatted_text TYPE 'I'.
  WRITE:/ formatted_text.
  WRITE 'Right'  TO formatted_text RIGHT-JUSTIFIED.
  "MESSAGE formatted_text TYPE 'I'.
  WRITE:/ formatted_text.

  "示例4:EXPONENT exp
  DATA: float          TYPE f.
  DATA: formatted_text1 TYPE c LENGTH 6.
  float = SQRT( 2 ).
  WRITE  float TO formatted_text1 EXPONENT 0.
  WRITE:/ formatted_text1.
  WRITE  float TO formatted_text1 EXPONENT 1.
  WRITE:/ formatted_text1.

  "示例5: NO—GROUPING,不添加千分符
  DATA: test_number(10) TYPE P DECIMALS 2.
  DATA: formatted_text2 TYPE c LENGTH 6.
  test_number = '3333.33'.
  WRITE test_number TO formatted_text2 NO-GROUPING.
  "*33.33,千分符变成*
  WRITE:/ formatted_text2.

  "示例6: NO-SIGN
  DATA: test_number1(10) TYPE P DECIMALS 2.
  DATA: test_number2 TYPE I.
  DATA: formatted_text3 TYPE c LENGTH 6.
  test_number1 = '3333.33'.
  test_number2 = -15.
  WRITE test_number1 TO formatted_text3 NO-SIGN.
  "*33.33?
  WRITE:/ formatted_text3.
  WRITE test_number2 TO formatted_text3 NO-SIGN.
  "15,负号去掉
  WRITE:/ formatted_text3.

  "示例7: NO-ZERO
  "不能同时用ENVIRONMENT TIME FORMAT and TIME ZONE.
  DATA: test_number3(10) TYPE N.
  DATA: formatted_text4 TYPE C LENGTH 20.
  test_number3 = 123.
  "0000000123添加了前置0
  WRITE test_number3 TO formatted_text4.
  WRITE:/ formatted_text4.
  "去掉前置0
  WRITE test_number3 TO formatted_text4 NO-ZERO.
  WRITE:/ formatted_text4.

  "示例8:CURRENCY
  "table:TCURX
  " ENVIRONMENT TIME FORMAT and TIME ZONE
  "and not for the numerical data types decfloat16 and decfloat34
  DATA: test_int  TYPE i VALUE 12345678.
  DATA: formatted_text5 TYPE c LENGTH 10.
  WRITE test_int TO formatted_text5 CURRENCY 'EUR'.
  WRITE:/ formatted_text5.

  "示例9:DECIMALS dec
  "不能同时用ENVIRONMENT TIME FORMAT, TIME ZONE, and UNIT.
  DATA: test_pack TYPE p LENGTH 8 DECIMALS 4 VALUE '1234.5678'.
  DATA: formatted_text6 TYPE c LENGTH 10.
  "两位小数
  WRITE test_pack TO formatted_text6 DECIMALS 2.
  WRITE:/ formatted_text6.


  "示例10:ROUND
  "不能同时用STYLE, ENVIRONMENT TIME FORMAT, TIME ZONE, and UNIT
  DATA: test_pack1 TYPE p LENGTH 8 DECIMALS 0 VALUE '12345678'.
  DATA: formatted_text7 TYPE c LENGTH 12.
  "123,457
  WRITE test_pack1 TO formatted_text7 ROUND 2.
  WRITE:/ formatted_text7.
  "12,346
  WRITE test_pack1 TO formatted_text7 ROUND 3.
  WRITE:/ formatted_text7.

  "示例11:UNIT
  "表:T006 字段MSEHI
  "不能同时用DECIMALS, ROUND, STYLE, ENVIRONMENT TIME FORMAT, and TIME ZONE
  DATA: test_pack2 TYPE p LENGTH 8 DECIMALS 4 VALUE '1234.0000'.
  DATA: formatted_text8 TYPE c LENGTH 12.
  WRITE test_pack2 TO formatted_text8 UNIT '%'.
  WRITE:/ formatted_text8.

  "示例12:
  "ENVIRONMENT TIME FORMAT
  "不能同时用CURRENCY, DECIMALS, EXPONENT, NO-GROUPING, NO-SIGN,
  "NO-ZERO, ROUND, STYLE, TIME ZONE, or UNIT
  "TIME ZONE tz
  "不能同时使用CURRENCY, DECIMALS, ENVIRONMENT TIME FORMAT, EXPONENT,
  "NO-GROUPING, NO-SIGN, NO-ZERO, ROUND, STYLE, or UNIT
  "表TTZZ
  DATA: time_stamp     TYPE timestamp.
  DATA: tzone          TYPE timezone.
  DATA: formatted_text9 TYPE c LENGTH 50.
  time_stamp = 20100627180000.
  tzone      = 'AUSTAS'.
  WRITE time_stamp TO formatted_text9 TIME ZONE tzone.
  WRITE:/ formatted_text9.

  "示例13
  "STYLE stl
  "不能同时使用CURRENCY, DD/MM/YY, ... , YYMMDD, ROUND,
  "ENVIRONMENT TIME FORMAT, TIME ZONE, and UNIT
  "获取format
  DATA: cldescr  TYPE REF TO cl_abap_classdescr,
        formats  TYPE abap_attrdescr_tab,
        format   LIKE LINE OF formats.
  FIELD-SYMBOLS <format> LIKE cl_abap_format=>o_scientific.
  DATA: BEGIN OF s_format,
          value LIKE cl_abap_format=>e_xml_text,
          name  TYPE abap_attrdescr-name,
        END OF s_format.
  DATA:lt_formats LIKE SORTED TABLE OF s_format WITH UNIQUE KEY value.
  DATA:lv_number TYPE decfloat34 VALUE '123456789.3146'.
  DATA:formatted_text10 TYPE C LENGTH 20.

  cldescr ?= cl_abap_classdescr=>describe_by_name( 'CL_ABAP_FORMAT' ).
  formats = cldescr->attributes.
  DELETE formats WHERE name NP 'O_*' OR is_constant <> 'X'.
  "获取format
*  O_SIMPLE
*  O_SIGN_AS_POSTFIX
*  O_SCALE_PRESERVING
*  O_SCIENTIFIC
*  O_SCIENTIFIC_WITH_LEADING_ZERO
*  O_SCALE_PRESERVING_SCIENTIFIC
*  O_ENGINEERING
  LOOP AT formats INTO format.
    ASSIGN cl_abap_format=>(format-name) TO <format>.
    s_format-value = <format>.
    s_format-name = format-name.
    INSERT s_format INTO TABLE lt_formats.
  ENDLOOP.

  LOOP AT lt_formats INTO s_format.
    WRITE lv_number STYLE s_format-value TO formatted_text10.
    WRITE:/ formatted_text10.
  ENDLOOP.


  "示例14:USING { {NO EDIT MASK}|{EDIT MASK mask} }
  DATA: dura           TYPE i.
  DATA: time           TYPE t VALUE '080000'.
  DATA: formatted_text11 TYPE c LENGTH 30.
  dura = sy-uzeit - time.
  time = dura.
  "function:CONVERSION_EXIT_DURA_OUTPUT,==开头
  WRITE dura TO formatted_text11 USING EDIT MASK '==SDURA'.
  WRITE:/ formatted_text11.
  "RR:靠右,LL:靠左,_会被替换
  WRITE time TO formatted_text11 USING EDIT MASK 'RRThe duration is __:__:__'.
  WRITE:/ formatted_text11.

  "示例15:
*  DD/MM/YY   | MM/DD/YY
*  | DD/MM/YYYY | MM/DD/YYYY
*  | DDMMYY     | MMDDYY
*  | YYMMDD
  DATA formatted_text12 TYPE c LENGTH 50.
  WRITE sy-datlo TO formatted_text12 YYMMDD.
  WRITE:/ formatted_text12.
  WRITE sy-datlo TO formatted_text12 DD/MM/YYYY.
  WRITE:/ formatted_text12.

ENDFORM.

 

 

上一篇:【2020版PHP高级教程-Swoole4.X从入门到精通全套高级实战】


下一篇:swoole4-swoole创建Mysql连接池