Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Rounding a value to a specific factor.

Former Member
0 Likes
983

Hi guys/dolls

I’ve a small rounding problem; I need to round my figures to the next nearest £5.

Thus 80852.04 becomes 80855.00

Some more examples are

80852.04 --> 80855.00

64938.60 --> 60940.00

29928.04 --> 29930.00

Is there a generic rounding function to do this and I need to be able to round it to any specific value i.e. not only £5 (I chose this as an example) it could be £10 in which case the above figures would be as follows:-

80852.04 --> 80860.00

64938.60 --> 60940.00

29928.04 --> 29930.00

Some example code would be useful.

I have made an attempt but the code below doesn't’t seem to work for all cases plus I guess there is a more elegant way of doing it. Any help would be appreciated.

  • This procedure will roundup according to the roundup value depicted

  • on the front screen.

CONSTANTS: sen_staff_roundup(2) TYPE n VALUE '5',

gen_staff_roundup(2) TYPE n VALUE '1'.

DATA : l_whole_part TYPE i,

l_modulus_part TYPE i.

  • If no roundup value specified then for

  • Professorial/Grade6 & Registrar value rounded up to

  • SEN_STAFF_ROUNDUP value otherwise assume its 1 for all others.

IF p_rndto = ''.

CASE p0008-trfgr.

WHEN cprof OR cgrd6 OR creg.

p_rndto = sen_staff_roundup.

WHEN OTHERS.

p_rndto = gen_staff_roundup.

ENDCASE.

ENDIF.

l_whole_part = annual_increased_salary DIV p_rndto.

l_modulus_part = annual_increased_salary MOD p_rndto.

IF l_modulus_part <> 0.

l_whole_part = l_whole_part + 1.

ENDIF.

rounded_ann_sal = l_whole_part * p_rndto.

Many thanks in advance.

Raj

Hi guys/dolls

I’ve a small rounding problem; I need to round my figures to the next nearest £5.

Thus 80852.04 becomes 80855.00

Some more examples are

80852.04 --> 80855.00

64938.60 --> 60940.00

29928.04 --> 29930.00

Is there a generic rounding function to do this and I need to be able to round it to any specific value i.e. not only £5 (I chose this as an example) it could be £10 in which case the above figures would be as follows:-

80852.04 --> 80860.00

64938.60 --> 60940.00

29928.04 --> 29930.00

Some example code would be useful.

I have made an attempt but the code below doesn't’t seem to work for all cases plus I guess there is a more elegant way of doing it. Any help would be appreciated.

  • This procedure will roundup according to the roundup value depicted

  • on the front screen.

CONSTANTS: sen_staff_roundup(2) TYPE n VALUE '5',

gen_staff_roundup(2) TYPE n VALUE '1'.

DATA : l_whole_part TYPE i,

l_modulus_part TYPE i.

  • If no roundup value specified then for

  • Professorial/Grade6 & Registrar value rounded up to

  • SEN_STAFF_ROUNDUP value otherwise assume its 1 for all others.

IF p_rndto = ''.

CASE p0008-trfgr.

WHEN cprof OR cgrd6 OR creg.

p_rndto = sen_staff_roundup.

WHEN OTHERS.

p_rndto = gen_staff_roundup.

ENDCASE.

ENDIF.

l_whole_part = annual_increased_salary DIV p_rndto.

l_modulus_part = annual_increased_salary MOD p_rndto.

IF l_modulus_part <> 0.

l_whole_part = l_whole_part + 1.

ENDIF.

rounded_ann_sal = l_whole_part * p_rndto.

Many thanks in advance.

Raj

3 REPLIES 3
Read only

Former Member
0 Likes
787

Hi,

Perhaps you can try to use mathematical function CEIL and FLOOR.

Please check the following for more detail.

Mathematical Functions:

ABAP contains a range of built-in functions that you can use as mathematical expressions, or as part of a mathematical expression:

[COMPUTE] <n> = <func>( <m> ).

The blanks between the parentheses and the argument <m> are obligatory. The result of calling the function <func> with the argument <m> is assigned to <n>.

Functions for all Numeric Data Types

The following built-in functions work with all three numeric data types (F, I, and P) as arguments.

Functions for all numeric data types

Function

Result

ABS

Absolute value of argument.

SIGN

Sign of argument: 1 X > 0

SIGN( X) = 0 if X = 0

-1 X < 0

CEIL

Smallest integer value not smaller than the argument.

FLOOR

Largest integer value not larger than the argument.

TRUNC

Integer part of argument.

FRAC

Fraction part of argument.

The argument of these functions does not have to be a numeric data type. If you choose another type, it is converted to a numeric type. For performance reasons, however, you should use the correct type whenever possible. The functions itself do not have a data type of their own. They do not change the numerical precision of a numerical operation.

DATA N TYPE P DECIMALS 2.

DATA M TYPE P DECIMALS 2 VALUE '-5.55'.

N = ABS( M ). WRITE: 'ABS: ', N.

N = SIGN( M ). WRITE: / 'SIGN: ', N.

N = CEIL( M ). WRITE: / 'CEIL: ', N.

N = FLOOR( M ). WRITE: / 'FLOOR:', N.

N = TRUNC( M ). WRITE: / 'TRUNC:', N.

N = FRAC( M ). WRITE: / 'FRAC: ', N.

The output appears as follows:

ABS: 5.55

SIGN: 1.00-

CEIL: 5.00-

FLOOR: 6.00-

TRUNC: 5.00-

FRAC: 0.55-

Regards,

Ferry Lianto

Read only

Former Member
0 Likes
787

Hi ,



parameters : p1 type p decimals 2,

data : q  type i,
       r type c .
 
 q = trunc( p1 ).

        r = q mod 10.
         if r <= 4.            " i took 4 for tolerence
            q = q + ( 5 - r ).
         elseif r >= 5.        " same here
            q = q +  ( 10 - r ) . 
         endif.

          p1  = q.

          write:/ p1.

Execute this code to see the rounding for ur query .

Regards,

Vijay

Read only

Former Member
0 Likes
787

You can use function module HR_ECM_ROUND_AMOUNT. Pass 'B' as the rounding type and '5' as the rounding amount. Your original value should be the raw amount. I hope this helps.

DATA: wa_message_handler TYPE REF TO IF_HRPA_MESSAGE_HANDLER.

CALL FUNCTION 'HR_ECM_ROUND_AMOUNT'

EXPORTING

raw_amount = raw_ann_sal

rdtyp = 'B'

rdamt = '5'

message_handler = wa_message_handler

IMPORTING

ROUNDED_AMOUNT = rnd_ann_sal

  • IS_OK =

.

- April King

Message was edited by:

April King