导语:最近写报表的时候,顾问想把小数展示换城分数展示,例如0.75显示为3/4,哈哈哈哈,有点奇怪,实现起来还挺好玩的,要多考虑一些情况,封装了一个函数,下面贴一下代码。
一、函数信息:
字段 | 描述 | 属性 | |
---|---|---|---|
函数名称 | 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 && '/' && 1.
iv_fraction_y = lv_int && '/' && 1.
iv_fraction_all = lv_int && '/' && 1.
iv_fraction_i = lv_int.
ENDIF.
ENDIF.
ENDFUNCTION.
三、效果示例
作者:小飞猪猪猪猪猪猪猪–CSDN