Skip to content

Commit 49fcc05

Browse files
authored
Generalize get_max_value method for packed numbers (#893)
1 parent 68c8626 commit 49fcc05

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

src/abap/cl_abap_exceptional_values.clas.abap

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@ ENDCLASS.
1616
CLASS cl_abap_exceptional_values IMPLEMENTATION.
1717

1818
METHOD get_max_value.
19-
DATA lv_type TYPE c LENGTH 1.
20-
DATA lv_length TYPE i.
21-
DATA lv_decimals TYPE i.
22-
FIELD-SYMBOLS <out> TYPE any.
19+
DATA lv_type TYPE c LENGTH 1.
20+
DATA lv_length TYPE i.
21+
DATA lv_decimals TYPE i.
22+
DATA lv_digits_before_decimal TYPE i.
23+
DATA lv_integer_part TYPE string.
24+
DATA lv_decimal_part TYPE string.
25+
FIELD-SYMBOLS <out> TYPE any.
2326

2427
DESCRIBE FIELD in TYPE lv_type.
2528

@@ -32,15 +35,20 @@ CLASS cl_abap_exceptional_values IMPLEMENTATION.
3235
CREATE DATA out TYPE p LENGTH lv_length DECIMALS lv_decimals.
3336
ASSIGN out->* TO <out>.
3437

35-
IF lv_length = 3 AND lv_decimals = 1.
36-
<out> = '9999.9'.
37-
ELSEIF lv_length = 4 AND lv_decimals = 1.
38-
<out> = '999999.9'.
39-
ELSEIF lv_length = 7 AND lv_decimals = 3.
40-
<out> = '9999999999.999'.
41-
ELSE.
42-
ASSERT 1 = 'todo'.
38+
lv_digits_before_decimal = lv_length * 2 - 1 - lv_decimals.
39+
40+
DO lv_digits_before_decimal TIMES.
41+
lv_integer_part = lv_integer_part && '9'.
42+
ENDDO.
43+
44+
IF lv_decimals > 0.
45+
lv_decimal_part = '.'.
46+
DO lv_decimals TIMES.
47+
lv_decimal_part = lv_decimal_part && '9'.
48+
ENDDO.
4349
ENDIF.
50+
51+
<out> = lv_integer_part && lv_decimal_part.
4452
WHEN OTHERS.
4553
WRITE '@KERNEL console.dir(INPUT);'.
4654
ASSERT 1 = 'todo'.

src/abap/cl_abap_exceptional_values.clas.testclasses.abap

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ CLASS ltcl_exceptional_values DEFINITION FOR TESTING RISK LEVEL HARMLESS DURATIO
77
METHODS max_packed1 FOR TESTING RAISING cx_static_check.
88
METHODS max_packed2 FOR TESTING RAISING cx_static_check.
99
METHODS max_packed3 FOR TESTING RAISING cx_static_check.
10+
METHODS max_packed4 FOR TESTING RAISING cx_static_check.
1011
METHODS min_packed FOR TESTING RAISING cx_static_check.
1112

1213
ENDCLASS.
@@ -49,6 +50,18 @@ CLASS ltcl_exceptional_values IMPLEMENTATION.
4950
exp = '999999.9' ).
5051
ENDMETHOD.
5152

53+
METHOD max_packed4.
54+
DATA foo TYPE p LENGTH 9 DECIMALS 5.
55+
DATA ref TYPE REF TO data.
56+
FIELD-SYMBOLS <field> TYPE any.
57+
ref = cl_abap_exceptional_values=>get_max_value( foo ).
58+
ASSIGN ref->* TO <field>.
59+
60+
cl_abap_unit_assert=>assert_equals(
61+
act = <field>
62+
exp = '999999999999.99999' ).
63+
ENDMETHOD.
64+
5265
METHOD min_packed.
5366
DATA max TYPE p LENGTH 3 DECIMALS 1.
5467
DATA ref TYPE REF TO data.

0 commit comments

Comments
 (0)