‎2012 Nov 02 7:32 AM
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
‎2012 Nov 02 7:48 AM
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
‎2012 Nov 02 8:54 AM
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
‎2012 Nov 02 9:04 AM
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.
‎2012 Nov 02 3:36 PM
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.
‎2012 Nov 02 9:15 AM
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.
‎2012 Nov 02 9:52 AM
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'.
‎2012 Nov 05 8:39 AM
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>.
‎2012 Nov 05 8:41 AM
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.
‎2012 Nov 05 8:44 AM
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...