SAP将小数转换为分数

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

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

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值