SAP将小数转换为分数

2023-10-13 18:42:56 浏览数 (1)

说明:主数据的业务顾问提出了一个需求是在ALV展示的时候把小数转为分数展示,这里写一个函数来实现。

函数名称:ZDECIMAL_TO_FRACTION 函数入参:IV_DECIMAL 函数出参:IV_FRACTION 未约分 IV_FRACTION_ALL 约分 IV_FRACTION_Y 小数位转换成分数 IV_FRACTION_I 整数表达 函数源代码:

代码语言:javascript复制
FUNCTION zdecimal_to_fraction.
*"----------------------------------------------------------------------
*"*"本地接口:
*"  IMPORTING
*"     REFERENCE(IV_DECIMAL)
*"  EXPORTING
*"     REFERENCE(IV_FRACTION)
*"     REFERENCE(IV_FRACTION_ALL)
*"     REFERENCE(IV_FRACTION_Y)
*"     REFERENCE(IV_FRACTION_I)
*"----------------------------------------------------------------------
  DATA : lv_change      TYPE char255.
  DATA : lv_int         TYPE char255.
  DATA : lv_dec         TYPE char255.
  DATA : lv_molecule    TYPE char255.  "分子
  DATA : lv_denominator TYPE char255.  "分母
  DATA : n              TYPE i.
  DATA : lv_len_dec     TYPE i.
  DATA : lv_len         TYPE i.
  DATA : lv_over.
  IF iv_decimal IS NOT INITIAL.

    lv_change = iv_decimal.
    CONDENSE lv_change.
    SPLIT  lv_change AT '.' INTO lv_int lv_dec.

    IF lv_dec <> 0 AND lv_dec IS NOT INITIAL.
      lv_len = strlen( lv_dec ).
      IF lv_len < 30.
        lv_molecule = lv_dec."分子赋值
        lv_denominator = 1. "分母赋值
        CONDENSE lv_denominator.
        DO lv_len TIMES.
          lv_denominator = 10 * lv_denominator.   lv_change = lv_change * 10.
          CONDENSE lv_denominator.
          CONDENSE lv_change.
        ENDDO.
        "未约分的数据【output】
        iv_fraction  = lv_change && '/' && lv_denominator.
        "小数部分转换成分数
        DO .
          IF lv_molecule MOD 2 = 0 AND lv_denominator MOD 2 = 0.
            lv_molecule    = lv_molecule    / 2.
            lv_denominator = lv_denominator / 2.
          ELSEIF lv_molecule MOD 3 = 0 AND lv_denominator MOD 3 = 0.
            lv_molecule    = lv_molecule    / 3.
            lv_denominator = lv_denominator / 3.
          ELSEIF lv_molecule MOD 5 = 0 AND lv_denominator MOD 5 = 0.
            lv_molecule    = lv_molecule    / 5.
            lv_denominator = lv_denominator / 5.
          ELSEIF lv_molecule MOD 7 = 0 AND lv_denominator MOD 7 = 0.
            lv_molecule    = lv_molecule    / 7.
            lv_denominator = lv_denominator / 7.
          ELSE.
            CONDENSE lv_molecule.
            CONDENSE lv_denominator.
            EXIT.
          ENDIF.
        ENDDO.
        "约分后的小数部分【output】
        iv_fraction_y = lv_molecule &&  '/' && lv_denominator.
        "整数表达式【output】
        iv_fraction_i = lv_int && '又' && lv_molecule &&  '/' && lv_denominator.

        "全部数据转换成分数
        lv_molecule = lv_int * lv_denominator   lv_molecule.
        DO .
          IF lv_molecule MOD 2 = 0 AND lv_denominator MOD 2 = 0.
            lv_molecule    = lv_molecule    / 2.
            lv_denominator = lv_denominator / 2.
          ELSEIF lv_molecule MOD 3 = 0 AND lv_denominator MOD 3 = 0.
            lv_molecule    = lv_molecule    / 3.
            lv_denominator = lv_denominator / 3.
          ELSEIF lv_molecule MOD 5 = 0 AND lv_denominator MOD 5 = 0.
            lv_molecule    = lv_molecule    / 5.
            lv_denominator = lv_denominator / 5.
          ELSEIF lv_molecule MOD 7 = 0 AND lv_denominator MOD 7 = 0.
            lv_molecule    = lv_molecule    / 7.
            lv_denominator = lv_denominator / 7.
          ELSE.
            CONDENSE lv_molecule.
            CONDENSE lv_denominator.
            EXIT.
          ENDIF.
        ENDDO.
        "约分后的小数部分【output】
        iv_fraction_all = lv_molecule &&  '/' && lv_denominator.
      ELSE.
        iv_fraction_y = '小数过小,无法计算!'.
        iv_fraction_i = '小数过小,无法计算!'.
      ENDIF.
    ELSE.
      iv_fraction      = lv_int && '/' && lv_int.
      iv_fraction_y    = lv_int && '/' && lv_int.
      iv_fraction_all  = lv_int && '/' && lv_int.
      iv_fraction_i    = lv_int.
    ENDIF.
  ENDIF.
ENDFUNCTION.

0 人点赞