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: 

How to round off decimal places?

Former Member
0 Kudos

Hi friends,

I am in a situation where i need to pass a currency field value Type P decimals 8 to

a variable of type c length 12.

As I have to write the output on application server.

Example

Value to be converted of Type P decimals 8 i.e. 5678.65800341

Value Required in Chararcter 12 with decimals 2 i.e. (9.2) total 12 characters. required value 5678.65 or 5678.66

Kindly help.

Will reward points to correct answers.

Thanks,

Pradeep.

1 ACCEPTED SOLUTION

Former Member

Hello,

Check this sample:

DATA: WA_INPUT TYPE P DECIMALS 8,

WA_OUTPUT TYPE P DECIMALS 2.

WA_INPUT = '5678.65800341'.

*

CALL FUNCTION 'ROUND'

EXPORTING

INPUT = WA_INPUT

IMPORTING

OUTPUT = WA_OUTPUT

EXCEPTIONS

INPUT_INVALID = 1

OVERFLOW = 2

TYPE_INVALID = 3

OTHERS = 4.

IF SY-SUBRC <> 0.

  • MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

  • WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

WRITE: WA_OUTPUT.

Regards,

Vasanth

8 REPLIES 8

Former Member
0 Kudos

hi,

you can try this:

DATA: I TYPE I,

P TYPE P DECIMALS 2 Value '3.5'.

I = P.

This will round of P to 4 (Business rounding).

plz reward points if helpful..

Former Member
0 Kudos

try writing ur packed variable to char variable...i.e.

write packedvar to charvar decimals 2.

Regards

Vasu

Former Member

Hello,

Check this sample:

DATA: WA_INPUT TYPE P DECIMALS 8,

WA_OUTPUT TYPE P DECIMALS 2.

WA_INPUT = '5678.65800341'.

*

CALL FUNCTION 'ROUND'

EXPORTING

INPUT = WA_INPUT

IMPORTING

OUTPUT = WA_OUTPUT

EXCEPTIONS

INPUT_INVALID = 1

OVERFLOW = 2

TYPE_INVALID = 3

OTHERS = 4.

IF SY-SUBRC <> 0.

  • MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

  • WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

WRITE: WA_OUTPUT.

Regards,

Vasanth

Former Member
0 Kudos

Include RV64A601

rounding rule for closest interger value

FORM FRM_KONDI_WERT_601.

data: da_xkwerx(15).

unpack xkwert to da_xkwerx.

*

move '00' to da_xkwerx+13.

*

*

*

pack da_xkwerx to xkwert.

data : lgort1 like lips-lgort .

data : d1(13) , d6(13) .

data : d2 type p decimals 2 .

data : d3 type p decimals 0 .

data : d4 type p decimals 2 .

data d5(2) .

data : d8(3) .

unpack xkwert to d1 .

d2 = d1 .

d3 = d2 .

unpack d3 to d6.

d5 = d6+11(2) .

if d5 >= 50 .

d8 = 100 - d5 .

d1 = d6 + d8 .

else .

d1 = d6 - d5 .

endif .

*pack d1 to xkwert .

select single lgort into lgort1 from lips where

vbeln = komp-vgbel and

posnr = komp-vgpos .

IF SY-SUBRC = 0 .

if lgort1 = 'JSTO'.

d1 = 0 .

pack d1 to xkwert.

else .

pack d1 to xkwert.

endif .

ELSE .

pack d1 to xkwert.

ENDIF .

Former Member
0 Kudos

Hi Pradeep,

see this code..

DATA : Num1 TYPE p DECIMALS 8,

Num2(16) TYPE p DECIMALS 2,

C Type CHAR10.

Num1 = '19.64632333'.

CALL FUNCTION 'ROUND'

EXPORTING

decimals = 2

input = Num1

IMPORTING

output = Num2

EXCEPTIONS

input_invalid = 1

overflow = 2

type_invalid = 3

OTHERS = 4.

MOVE Num2 to C.

WRITE C.

Reward points if helpful..

Regards,

Goutham.

Former Member

Hi All

Use the below code and it is so simple

For 2 decimal places

data(lv_round) = round(  val = '5678.65800341' dec = 2  ).


lv_round will be 5678.66


data(lv_round) = round(  val = '5678.65300341' dec = 2  ).


lv_round will be 5678.65


For 5 decimals


data(lv_round) = round(  val = '5678.65800741' dec = 5 ).


lv_round will be 5678.65801


data(lv_round) = round(  val = '5678.65800341' dec = 5 ).


lv_round will be 5678.65800


Thanks,

Murugan


Message was edited by: murugan bavani

0 Kudos

It will when you hardcoded the value but when you assign the variable to the value field it will not work as expected

0 Kudos

This is simple process of rounding but when you are passing value from a variable need to move it to string variable and then pass the string variable to the formula for VAL operator then it will work