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

Function to format numbers

Former Member
0 Likes
1,055

Hello,

I am trying to write a function that does the following 2 things:

If a number is an integer, first add .00 after the integer, then add zeros in front such that altoghther it makes 9 characters (e.g.: 12 -> 000012.00)

If a number is not an integer, then ensure it is 2 decimals by truncating whatever that is after the 2 decimals and add 0 in fron as well (e.g. 12.12345 -> 000012.12)

There would be no number larger than 6 digits (i.e., 9 characters altogether)

Does anyone know whether there is any existing function or method that would do the job or at least part of it?

Thanks!

Regards,

Anyi

1 ACCEPTED SOLUTION
Read only

ferry_lianto
Active Contributor
0 Likes
943

Hi Anyi,

Please try this.

DATA: NUM       TYPE P05_DEC15_4,
      ROUND     TYPE  MAXBT,
      RESULT(10) TYPE C.
                                                                        
MOVE '12.12345' TO NUM.
                                                                        
CALL FUNCTION 'HR_NL_ROUNDING'
  EXPORTING
    INPUT  = NUM
    METHOD = '1'
  IMPORTING
    OUTPUT = ROUND.
                                                                        
MOVE ROUND TO RESULT.
SHIFT RESULT RIGHT.
TRANSLATE RESULT USING ' 0'.
WRITE: / RESULT.

Regards,

Ferry Lianto

6 REPLIES 6
Read only

ferry_lianto
Active Contributor
0 Likes
943

Hi,

Please check FM HR_NL_ROUNDING.

Regards,

Ferry Lianto

Read only

0 Likes
943

Hello,

So now the rounding and 2 decimal bit is resolved, anyone has any suggestion regarding the adding zero part?

Thanks Ferry for the suggestion!

Regards,

Anyi

> Hi,

>

> Please check FM HR_NL_ROUNDING.

>

> Regards,

> Ferry Lianto

Read only

raja_thangamani
Active Contributor
0 Likes
943

use Function module "ROUND"

Raja T

Message was edited by:

Raja T

Read only

0 Likes
943

Err...I am more concerned with the 0s in front now...

Thanks anyways!

Anyi

> use Function module "ROUND"

>

> Raja T

>

> Message was edited by:

> Raja T

Read only

ferry_lianto
Active Contributor
0 Likes
943

Hi Anyi,

This FM will do rounding into 2 decimals then you need to fill zeros.

Regards,

Ferry Lianto

Read only

ferry_lianto
Active Contributor
0 Likes
944

Hi Anyi,

Please try this.

DATA: NUM       TYPE P05_DEC15_4,
      ROUND     TYPE  MAXBT,
      RESULT(10) TYPE C.
                                                                        
MOVE '12.12345' TO NUM.
                                                                        
CALL FUNCTION 'HR_NL_ROUNDING'
  EXPORTING
    INPUT  = NUM
    METHOD = '1'
  IMPORTING
    OUTPUT = ROUND.
                                                                        
MOVE ROUND TO RESULT.
SHIFT RESULT RIGHT.
TRANSLATE RESULT USING ' 0'.
WRITE: / RESULT.

Regards,

Ferry Lianto