Application Development 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: 

Dynamic Decimals in SAP SCRIPT

Former Member
0 Kudos

Hi Experts,

I have a requirement to set decimal places dynamically based on value in a variable in sap script.

For example 1 : we have decimal variable &Dec& = 2 (Value in Dec will change dynamically)

                      display value &Value&  = 10.255.

        Output should be 10.25 based on decimal value.

For example 2 : we have decimal variable &Dec& = 3 (Value in Dec will change dynamically)

                     display value &Value&  = 10.255.

        Output should be 10.255 based on decimal value.

Thanks & Regards,

Sivaji Kankanala.

7 REPLIES 7

Former Member
0 Kudos

Hi Naidu,

A better solution would be to have additional text field in the internal table.

Use the write statement to put the value in the text field with the desired number of decimals

and output the text field instead of the numeric cheers

Regards

david_carballido
Active Participant
0 Kudos

Hi, you can build a text variable, for example

PARAMETERS: p_dec TYP i.

DATA: g_num TYPE REF TO data,

            g_text TYPE char15.

FIELD-SYMBOLS: <fs> TYPE any.

START-OF-SELECTION.

* Build variable

CREATE DATA g_num TYPE p DECIMALS p_dec.

* Assign variable

ASSIGN g_num->* TO <fs>.

<fs> = 10 / 3.

* Assign dynamic variable to text variable

g_text = <fs>.

CONDENSE g_text NO-GAPS.

* Check result

WRITE <fs>.

You might use this example to create a dynamic variable to pass to your sapscript, I hope help you

Regards.

DCC

0 Kudos

Hi David,

Thanks a lot. Your reply solved my query.

BR.

0 Kudos

Hi David,

Thanks a lot!!!
It helped me in solving an issue.

BR,
Prathyusha

arindam_m
Active Contributor
0 Kudos

Hi,

Have a CODE node before display and say you have the value in 10.2556 then pass the value to a global variable using you precision value in another variable using WRITE.. TO with formatting options DECIMALS. Something like

WRITE <var1> TO <var2> DECIMALS <var_dec_presion>.

Then display in node value in <var2>.

Cheers,

Arindam

rosenberg_eitan
Active Contributor
0 Kudos

Hi,

Have you consider FM FLTP_CHAR_CONVERSION .

Format conversion: Floating point --> Character

This is very useful in QM applications.

Regards.

venkateswaran_k
Active Contributor
0 Kudos

Hi

If it you require the dynamic option between 2 decimals or 3 decimals only then do as follows.  (Though I feel this may not be good solution 🙂 )

1. Create three variables  l_val3  type  MENGE  ( quantity type with 3 decimals ) 

                                         l_val2  type  DMBTR  ( amount type with 2 decimals )

                                         l_val    type  C length 15.

2. Based on your value &DEC&

    assign the value to either of the above  to final value l_val and print l_val.

😕  IF  &DEC& = 2

       l_val2 = &your variable&

       l_val = l_val2

😕  ELSE

       l_val3 = &your variable&

😕     l_val = l_val3

😕  ENDIF.

😕  &l_val(RC)&

OR  You have to use the format statement as   &VALUE(R8.x)&   where x is your dynamic positions.

Regards,

Venkat