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

Dynamically assign the decimal point

Former Member
0 Likes
3,982

Hi all,

I got a requirement in which I need to declare a variable.

Ex: lv_dec type P decimals 2.

But the value of decimals part, here '2' should be populated, based on some field.

There is one table, in which the decimals would be maintatined.

Based on that I need to declare the decimal part of the variable.( Ex: lv_dec type P decimals "table-field")

Please help me with your suggestions to achieve this.

Thanks in advance.

Rgs,

Priya

9 REPLIES 9
Read only

RaymondGiuseppi
Active Contributor
0 Likes
2,347

Are you trying to re-invent the CURR (with CUKY) and QUAN (with UNIT) ddic types ?

( Read Data Types in the ABAP Dictionary in BC - ABAP Dictionary documentation)

Regards,

Raymond

Read only

ThomasZloch
Active Contributor
0 Likes
2,347

I was about to ask the same. Internally, you can only work with a fixed number of decimals. For output, this will be converted based on currency or unit, if done properly.

Please explain more about the context of your issue.


Thomas

Read only

Former Member
0 Likes
2,347

Hi,

To achieve this you can use the statement CREATE DATA:

CREATE DATA data TYPE typ LENGTH len DECIMALS dec.

Have a look to program DEMO_CREATE_SIMPLE_DATA...

Cheers,

Manu.

Read only

0 Likes
2,347

e.g.

PARAMETERS len TYPE i.

PARAMETERS dec TYPE i.

DATA dref TYPE REF TO data.

FIELD-SYMBOLS <f> TYPE any.

START-OF-SELECTION.

TRY.

     CREATE DATA dref TYPE p LENGTH len DECIMALS dec.

     ASSIGN dref->* TO <f>.

CATCH cx_sy_create_data_error.

ENDTRY.

--> <f> will have the length and decimal part defined on selection-screen.

Kr,

Manu.

Read only

former_member184635
Participant
0 Likes
2,347

Hi Priya,

I am not sure what exactly your requirement is ? but i think you need to place the decimal point in a number depending on the value of an field ( table field/Variable ) . I think this can be done by using some tricks of string function & diving the value with that variable so that decimal can be inserted accordingly.

Regards,

Dadarao.

Read only

kesavadas_thekkillath
Active Contributor
0 Likes
2,347

Is this alright for you ?

DATA:data_type TYPE REF TO cl_abap_elemdescr,

           wf_ref TYPE REF TO data.

  field-symbols:<fs> type any.

 

TRY.

        CALL METHOD cl_abap_elemdescr=>get_p

          EXPORTING

            p_length   = varlen                 "<----

            p_decimals = vardec            "<----

          RECEIVING

            p_result   = data_type.

      CATCH cx_parameter_invalid_range .

    ENDTRY.

 

CREATE DATA wf_ref TYPE HANDLE data_type.

CHECK wf_ref IS BOUND.

assign wf_ref->* to <fs>.

<fs> = '1234.56'.

Read only

Former Member
0 Likes
2,347

Here I am pasting some code; please refer to it. I wrote the code as i was preparing a report for downloading SAP characteristics details where diff char may have diff values with diff decimal poits and stuffs.

   METHOD fltp_packd_conversion.
* The following code dynamically craetes a packed data type with the required decimal places and then moves
* the data so that the formatting is exactly like the CT04 screen.

    DATA: lv_dobjk1    TYPE REF TO data,
          lv_dobjk2    TYPE REF TO data,
          lv_decimals  TYPE i.


    FIELD-SYMBOLS: <lfs_atflv1> TYPE any,
                   <lfs_atflv2> TYPE any.

    lv_decimals = im_decimal_places.


* create the data object
    IF lv_dobjk1 IS NOT BOUND.
      CREATE DATA lv_dobjk1 TYPE p DECIMALS lv_decimals.
    ENDIF.

    IF lv_dobjk2 IS NOT BOUND.
      CREATE DATA lv_dobjk2 TYPE p DECIMALS lv_decimals.
    ENDIF.
* assign the data object to a field symbol.
    ASSIGN lv_dobjk1->* TO  <lfs_atflv1>.
    ASSIGN lv_dobjk2->* TO  <lfs_atflv2>.

* move the field content to field symbol for formatting.
    IF  <lfs_atflv1> IS ASSIGNED.
      <lfs_atflv1> = im_value_from.
    ENDIF.

    IF  <lfs_atflv2> IS ASSIGNED.
      <lfs_atflv2> = im_value_to.
    ENDIF.

* copy the field symbol value back to the internal table field.
    IF <lfs_atflv1> IS INITIAL.
      ex_value_from = 0.
    ELSE.
      ex_value_from = <lfs_atflv1>.
    ENDIF.

    IF <lfs_atflv2> IS INITIAL.
      ex_value_to = 0.
    ELSE.
      ex_value_to = <lfs_atflv2>.
    ENDIF.


    UNASSIGN:  <lfs_atflv1>,
               <lfs_atflv2>.

Read only

Former Member
0 Likes
2,347

      DATA: lv_dobjk1    TYPE REF TO data,
          lv_decimals  TYPE i.

   * create the data object
    IF lv_dobjk1 IS NOT BOUND.
      CREATE DATA lv_dobjk1 TYPE p DECIMALS lv_decimals.
    ENDIF.

Read only

Venkat_Sesha
Product and Topic Expert
Product and Topic Expert
0 Likes
2,347

Create a Z field and Create a Conversion Exit so that the data population and handling this can be done there.  Check the below links

http://scn.sap.com/message/7177721

http://scn.sap.com/thread/1545726

Hope this helps...