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: 

Converting Float to P type without rounding

Former Member
0 Kudos

Hello people,

I'm looking for a function, or even a pease of code to convert F variables to P variables without rounding the value.

Thanks alot,

Daniel Marchena

Edited by: danielmp on Nov 28, 2011 1:22 PM

1 ACCEPTED SOLUTION

Former Member
0 Kudos

DATA: FLOAT TYPE f,

PACKED TYPE p.

DATA: GV_STR TYPE STRING,

GV_STR1 TYPE STRING,

GV_STR2 TYPE STRING.

GV_STR = FLOAT.

SPLIT GV_STR AT '.' INTO GV_STR1 GV_STR2.

CLEAR GV_STR.

CONCATENATE GV_STR1 '.' GV_STR2+0(3) INTO GV_STR. " here in this step 3 is the number of decimals you wish to have

PACKED = GV_STR.

WRITE PACKED.

Even like that, when i make the variable "PACKED" receive "GV_STR" it rounds the value.

Thanks

Edited by: danielmp on Nov 28, 2011 2:20 PM

8 REPLIES 8

former_member186052
Active Participant
0 Kudos

Hi,

Could you please elaborate more on the requirement.

Regards,

-Sandeep

raymond_giuseppi
Active Contributor
0 Kudos

If you try to convert a floating field value with, say 9 decimals, to a packed type with 2 or 4 decimals without rounding, you can look for a long time... (read [Conversion Rules for Elementary Data Types|http://help.sap.com/abapdocu_70/en/ABENCONVERSION_ELEMENTARY.htm] for [Conversion table for source field type f|http://help.sap.com/abapdocu_70/en/ABENCONVERSION_TYPE_F.htm] )

AFAIK conversion from floating point is always subject to rounding (?) So try to convert the result back to floating and if this new value is greater than the original value, subtract 1 to the result of the first conversion (at rightmost position of decimal)

Regards,

Raymond

Former Member
0 Kudos

I have a f(15) value 98.9898989 and i want to pass it to a p(15) decimals 3 for example, without rounding.

thanks.

0 Kudos

Hi,

I have no idea of any function module or something doing that but I have a method...

Follow below steps:

DATA: GV_STR TYPE STRING,

GV_STR1 TYPE STRING,

GV_STR2 TYPE STRING.

1. Copy your float type to a string say GV_STR.

2. Use SPLIT 'GV_STR' AT '.' INTO GV_STR1 GV_STR2.

3. CLEAR GV_STR.

4. CONCATENATE GV_STR1 '.' GV_STR2+0(3) INTO GV_STR. " here in this step 3 is the number of decimals you wish to have

5. YOUR TYPE P FIELD = GV_STR.

Hope this helps.

Regards,

-Sandeep

Former Member
0 Kudos

DATA: FLOAT TYPE f,

PACKED TYPE p.

DATA: GV_STR TYPE STRING,

GV_STR1 TYPE STRING,

GV_STR2 TYPE STRING.

GV_STR = FLOAT.

SPLIT GV_STR AT '.' INTO GV_STR1 GV_STR2.

CLEAR GV_STR.

CONCATENATE GV_STR1 '.' GV_STR2+0(3) INTO GV_STR. " here in this step 3 is the number of decimals you wish to have

PACKED = GV_STR.

WRITE PACKED.

Even like that, when i make the variable "PACKED" receive "GV_STR" it rounds the value.

Thanks

Edited by: danielmp on Nov 28, 2011 2:20 PM

0 Kudos

Heyy

you just specified PACKED type P.

so there will be no decimal points...

please specify as PACKED TYPE P LENGHT <AS REQUIRED> DECIMALS ❤️ or as required>.

I Tried using 3 for your given example of 98.9898989

it printed 98.989 for me.

Regards,

-Sandeep

0 Kudos

Hi,

Try this one,


REPORT.

DATA: YOUR_DEC TYPE N LENGTH 02 VALUE 03.
DATA: F1 TYPE F,
      P1 TYPE P DECIMALS 3,
      C1 TYPE C LENGTH 256,
      S1 TYPE STRING,
      S2 TYPE STRING,
      S3 TYPE STRING.

F1 = '9999999999.9898989'.

WRITE F1 TO C1 EXPONENT 0.
CONDENSE C1 NO-GAPS.

SPLIT C1 AT '.' INTO S1 S2.
CONCATENATE S1 S2+0(YOUR_DEC) INTO S3 SEPARATED BY '.'.
P1 = S3.
WRITE P1.

Hope this help,

Tiwa N.

Edited by: Tiwa Noitawee on Nov 28, 2011 3:45 PM

Former Member
0 Kudos

Thank's a lot everyone.

The following code solved my problem:

REPORT ZNAME.

DATA: FLOAT TYPE f,

PACKED(15) TYPE p DECIMALS 3.

DATA: GV_STR TYPE STRING,

GV_STR1 TYPE STRING,

GV_STR2 TYPE STRING.

GV_STR = FLOAT.

SPLIT GV_STR AT '.' INTO GV_STR1 GV_STR2.

CLEAR GV_STR.

CONCATENATE GV_STR1 '.' GV_STR2+0(3) INTO GV_STR. " here in this step 3 is the number of decimals you wish to have

PACKED = GV_STR.

WRITE PACKED.