Skip to Content
avatar image
Former Member

How to round off decimal places?

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.

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

6 Answers

  • Best Answer
    avatar image
    Former Member
    Jan 15, 2008 at 11:26 AM

    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

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    May 10, 2016 at 10:27 AM

    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

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jan 15, 2008 at 11:22 AM

    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..

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jan 15, 2008 at 11:23 AM

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

    write packedvar to charvar decimals 2.

    Regards

    Vasu

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jan 15, 2008 at 11:27 AM

    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 .

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jan 15, 2008 at 11:32 AM

    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.

    Add comment
    10|10000 characters needed characters exceeded