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

Trancate Type P value

former_member189596
Active Participant
0 Likes
1,542

Hi all,

i am facing problem with Trancation of Type P value

I have P type variable V1 with Lenght 30 decimal 14.

So i am geting value like 444.99999988

i Have another P type variable V2 with lenght 13 decial 2.

I need to populate variable V2 with value of V1 like 444.99...Means i need to trancate value after decimal with 2 places.

Let me know how to achive this .

Thanks in advance,

regards,

JBR

1 ACCEPTED SOLUTION
Read only

viquar_iqbal
Active Contributor
0 Likes
1,163

Hi

You can use ROUND off addtion if you want to round off the value of V1.

Or else it check this example taken from sap help.

DATA pack TYPE p LENGTH 8 DECIMALS 4 VALUE '1234.5678'. 
WRITE pack NO-GROUPING DECIMALS 2
o/p 1234,57

Thanks

Viquar Iqbal

Hi all,

i am facing problem with Trancation of Type P value

I have P type variable V1 with Lenght 30 decimal 14.

So i am geting value like 444.99999988

i Have another P type variable V2 with lenght 13 decial 2.

I need to populate variable V2 with value of V1 like 444.99...Means i need to trancate value after decimal with 2 places.

Let me know how to achive this .

Thanks in advance,

regards,

JBR

6 REPLIES 6
Read only

viquar_iqbal
Active Contributor
0 Likes
1,164

Hi

You can use ROUND off addtion if you want to round off the value of V1.

Or else it check this example taken from sap help.

DATA pack TYPE p LENGTH 8 DECIMALS 4 VALUE '1234.5678'. 
WRITE pack NO-GROUPING DECIMALS 2
o/p 1234,57

Thanks

Viquar Iqbal

Read only

0 Likes
1,163

Hi,

Thanks for quick replay,

data : V1 type P lenght 30 decimal 14,

V2 type p lenght 13 deciamls 2.

The values of V1 comes like

V1 = 44449.99988 and 0.99988 and 9.9998888 and 1.23678 0.99887.

The result value we are expecting from v1 like

V2 = 44449.99 and 0.99 and 9.99 and 1.23 , 0.99.

Let me know how can i achive exact V2 values from V1 values .

thanks &Reagrds,

JBR

Read only

0 Likes
1,163

All you need is to subtract value 0.005 so round will work fine in all cases either over 5 (which rounds to next full value or below 5 which rounds to floor full value)

I.e. value 44449.99988 - 0.005 = 4449.99488 -> will round down to 44449.99000 -> after decimals will be truncated the value is 4449.99

the same applies if third place is below 5

i.e. value 1.23278 - 0.005 = 1.22878 -> will round up to 1.2300 -> decimals will be truncated so you have 1.23


v1 = '44449.99988'.
v2 = v1 - '0.005' .  
WRITE: / v1, v2.

v1 = '0.99988'.
v2 = v1 - '0.005' .
WRITE: / v1, v2.

v1 = '9.9998888'.
v2 = v1 - '0.005' .
WRITE: / v1, v2.

v1 = '1.23678'.
v2 = v1 - '0.005' .
WRITE: / v1, v2.

v1 = '0.99887'.
v2 = v1 - '0.005' .
WRITE: / v1, v2.

Btw. Length 30 for P is not allowed. 16 is the max.

Regards

Marcin

Tested, works fine.

Edited by: Marcin Pciak on Aug 13, 2009 1:49 PM

Read only

0 Likes
1,163

Hi,

One way is to move the value to type c variable and split and concatenate with offsets.



data :  V1(16)  type P decimals 14,
         V2(16) type c,
       V3(13) type c,
       a(10) type c,
       b(10) type c.
       
       
       
v1 = '44449.99988'.

v2 = v1.

split v2 at '.' into a b.

concatenate a '.' b+0(2) into v3.

write v3.

Also note that the maximum length that can be used for type p is 16 for the whole value

Regards,

Vik

Read only

0 Likes
1,163

Hi,

Thanks for replay

the V1 values are not same always.

please see below code :

I am using this BAPI for geting all the characteristics for DENSITY

CALL FUNCTION 'BAPI_BUS1077_GETDETAIL'

EXPORTING

scenario = '01'

key_date = sy-datum

flg_prop_data = 'X'

TABLES

return = l_it_return

sub_header = l_it_sub_header

prop_header = l_it_prop_header

prop_data = l_it_prop_data.

Now i Have data l_it_prop_data .

Here the density charactestics values are FLOAT type

READ TABLE l_it_prop_data INTO l_wa_prop_data

WITH KEY recno_root = l_wa_estmj-recnroot

name_char = 'SAP_EHS_1013_005_VALUE'.

IF sy-subrc IS INITIAL.

CALL FUNCTION 'MD_CONV_QUANTITY_FLOAT_TO_PACK'

EXPORTING

iv_menge = l_wa_prop_data-num_val_fm

IMPORTING

EV_MENGE = l_v_dummy.

endif.

l_v_Dummy has type P value lenght 30decimals.

Now using this value, i need to populate P type variable lenght 13 deciamls 2.

Now can i achive this .

Thanks &Regards,

JBR

Read only

0 Likes
1,163

the V1 values are not same always.

No one has assumed here that V1 is constant.

Above code works fine to your requirement. Did you try it at all?


data: f_input type f VALUE '54565464.656756756',        "you can enter any F value
      p_output type mdr1menge.     "your type p 30 length dec 14

CALL FUNCTION 'MD_CONV_QUANTITY_FLOAT_TO_PACK'
  EXPORTING
    iv_menge       = f_input
 IMPORTING
   EV_MENGE       = p_output.  


data: p_round TYPE p LENGTH 13 DECIMALS 2.

p_round = p_output - '0.005'.  "p_round holds what you need now

write: / p_output , p_round.  

Regards

Marcin