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

Rounding Function

Former Member
0 Likes
3,848

I am bringing in external data and stuffing it directly into a table. The

external data has a price field that is 4 decimals and I am putting it into

a field that has only 2 decimal places. It seems that instead of rounding,

the last 2 places are being truncated. This is causing a small variance

when I compare the totals before and after the migration. What I need is a

rounding function that will put the correct value into the field?

1 ACCEPTED SOLUTION
Read only

Former Member
1,349

HI asish,

Rounding function

Try any of these, though #1 is probably yr best bet:

(1) ROUND - round a number

The SIGN parameter determines the rounding type:

Value Meaning

+ Round up (10.55 => 10.60)

- Round down (10.55 => 10.50)

X Commercial (10.55 => 10.60)

' ' No rounding (10.55 => 10.55)

(2) ROUND_AMOUNT - rounding based on company code and currency

(3) HR_ROUND_NUMBER - rounding rules defined in T559R

ABAP Code - Rounding Up

Pls have a look at the following.

The function CEIL or FLOOR might help.

The ABS, SIGN, CEIL, FLOOR, TRUNC, and FRAC Functions

DATA: I TYPE I,

P TYPE P DECIMALS 2,

M TYPE F VALUE '-3.5',

D TYPE P DECIMALS 1.

P = ABS( M ). " 3,5

I = P. " 4 - business rounding

I = M. " -4

I = CEIL( P ). " 4 - next largest whole number

I = CEIL( M ). " -3

I = FLOOR( P ). " 3 - next smallest whole number

I = FLOOR( M ). " -4

I = TRUNC( P ). " 3 - integer part

I = TRUNC( M ). " -3

D = FRAC( P ). " 0.5 - decimal part

D = FRAC( M ). " -0.5

Formatting Options

You can use various formatting options with the WRITE statement.

Syntax

WRITE .... <f> <option>.

Formatting options for all data types

Option

Function

LEFT-JUSTIFIED

Output is left-justified.

CENTERED

Output is centered.

RIGHT-JUSTIFIED

Output is right-justified.

UNDER <g>

Output starts directly under field <g>.

NO-GAP

The blank after field <f> is omitted.

USING EDIT MASK <m>

Specifies format template <m>.

USING NO EDIT MASK

Deactivates a format template specified in the ABAP Dictionary.

NO-ZERO

If a field contains only zeros, these are replaced by blanks. For type C and N fields, leading zeros are replaced automatically.

Formatting options for numeric fields

Option

Function

NO-SIGN

The leading sign is not displayed on the screen.

DECIMALS <d>

<d> defines the number of digits after the decimal point.

EXPONENT <e>

In type F fields, the exponent is defined in <e>.

ROUND <r>

Type P fields are multiplied by 10**(-r) and then rounded.

CURRENCY <c>

Format according to currency <c> in table TCURX.

UNIT <u>

The number of decimal places is fixed according to unit <u> specified in table T006 for type P fields.

Formatting options for date fields

Option

Function

DD/MM/YY

Separators as defined in user’s master record.

MM/DD/YY

Separators as defined in user’s master record.

DD/MM/YYYY

Separators as defined in user’s master record.

MM/DD/YYYY

Separators as defined in user’s master record.

DDMMYY

No separators.

MMDDYY

No separators.

YYMMDD

No separators.

For more information on formatting options and the exclusion principles for some of these options, see the keyword documentation of the WRITE statement.

Below are some examples of formatting options. For more examples, see Creating Complex Lists. The decimal character and thousands separators (period or comma) of numeric fields are defined in the user’s master record

ABAP Code

Screen output

DATA: g(5) TYPE c VALUE 'Hello',

f(5) TYPE c VALUE 'Dolly'.

WRITE: g, f.

WRITE: /10 g,

/ f UNDER g.

WRITE: / g NO-GAP, f.

Hello Dolly

Hello

Dolly

HelloDolly

DATA time TYPE t VALUE '154633'.

WRITE: time,

/(8) time USING EDIT MASK '__:__:__'.

154633

15:46:33

WRITE: '000123',

/ '000123' NO-ZERO.

000123

123

DATA float TYPE f VALUE '123456789.0'.

WRITE float EXPONENT 3.

123456,789E+03

DATA pack TYPE p VALUE '123.456'

DECIMALS 3.

WRITE pack DECIMALS 2.

WRITE: / pack ROUND -2,

/ pack ROUND -1,

/ pack ROUND 1,

/ pack ROUND 2.

123,46

12.345,600

1.234,560

12,346

1,235

WRITE: sy-datum,

/ sy-datum yymmdd.

27.06.1995

950627

Apart from the formatting options shown in the above tables, you can also use the formatting options of the FORMAT statement. These options allow you to specify the intensity and color of your output. For more information, see The FORMAT Statement

thanks

karthik

reward me if usefull

3 REPLIES 3
Read only

Former Member
1,350

HI asish,

Rounding function

Try any of these, though #1 is probably yr best bet:

(1) ROUND - round a number

The SIGN parameter determines the rounding type:

Value Meaning

+ Round up (10.55 => 10.60)

- Round down (10.55 => 10.50)

X Commercial (10.55 => 10.60)

' ' No rounding (10.55 => 10.55)

(2) ROUND_AMOUNT - rounding based on company code and currency

(3) HR_ROUND_NUMBER - rounding rules defined in T559R

ABAP Code - Rounding Up

Pls have a look at the following.

The function CEIL or FLOOR might help.

The ABS, SIGN, CEIL, FLOOR, TRUNC, and FRAC Functions

DATA: I TYPE I,

P TYPE P DECIMALS 2,

M TYPE F VALUE '-3.5',

D TYPE P DECIMALS 1.

P = ABS( M ). " 3,5

I = P. " 4 - business rounding

I = M. " -4

I = CEIL( P ). " 4 - next largest whole number

I = CEIL( M ). " -3

I = FLOOR( P ). " 3 - next smallest whole number

I = FLOOR( M ). " -4

I = TRUNC( P ). " 3 - integer part

I = TRUNC( M ). " -3

D = FRAC( P ). " 0.5 - decimal part

D = FRAC( M ). " -0.5

Formatting Options

You can use various formatting options with the WRITE statement.

Syntax

WRITE .... <f> <option>.

Formatting options for all data types

Option

Function

LEFT-JUSTIFIED

Output is left-justified.

CENTERED

Output is centered.

RIGHT-JUSTIFIED

Output is right-justified.

UNDER <g>

Output starts directly under field <g>.

NO-GAP

The blank after field <f> is omitted.

USING EDIT MASK <m>

Specifies format template <m>.

USING NO EDIT MASK

Deactivates a format template specified in the ABAP Dictionary.

NO-ZERO

If a field contains only zeros, these are replaced by blanks. For type C and N fields, leading zeros are replaced automatically.

Formatting options for numeric fields

Option

Function

NO-SIGN

The leading sign is not displayed on the screen.

DECIMALS <d>

<d> defines the number of digits after the decimal point.

EXPONENT <e>

In type F fields, the exponent is defined in <e>.

ROUND <r>

Type P fields are multiplied by 10**(-r) and then rounded.

CURRENCY <c>

Format according to currency <c> in table TCURX.

UNIT <u>

The number of decimal places is fixed according to unit <u> specified in table T006 for type P fields.

Formatting options for date fields

Option

Function

DD/MM/YY

Separators as defined in user’s master record.

MM/DD/YY

Separators as defined in user’s master record.

DD/MM/YYYY

Separators as defined in user’s master record.

MM/DD/YYYY

Separators as defined in user’s master record.

DDMMYY

No separators.

MMDDYY

No separators.

YYMMDD

No separators.

For more information on formatting options and the exclusion principles for some of these options, see the keyword documentation of the WRITE statement.

Below are some examples of formatting options. For more examples, see Creating Complex Lists. The decimal character and thousands separators (period or comma) of numeric fields are defined in the user’s master record

ABAP Code

Screen output

DATA: g(5) TYPE c VALUE 'Hello',

f(5) TYPE c VALUE 'Dolly'.

WRITE: g, f.

WRITE: /10 g,

/ f UNDER g.

WRITE: / g NO-GAP, f.

Hello Dolly

Hello

Dolly

HelloDolly

DATA time TYPE t VALUE '154633'.

WRITE: time,

/(8) time USING EDIT MASK '__:__:__'.

154633

15:46:33

WRITE: '000123',

/ '000123' NO-ZERO.

000123

123

DATA float TYPE f VALUE '123456789.0'.

WRITE float EXPONENT 3.

123456,789E+03

DATA pack TYPE p VALUE '123.456'

DECIMALS 3.

WRITE pack DECIMALS 2.

WRITE: / pack ROUND -2,

/ pack ROUND -1,

/ pack ROUND 1,

/ pack ROUND 2.

123,46

12.345,600

1.234,560

12,346

1,235

WRITE: sy-datum,

/ sy-datum yymmdd.

27.06.1995

950627

Apart from the formatting options shown in the above tables, you can also use the formatting options of the FORMAT statement. These options allow you to specify the intensity and color of your output. For more information, see The FORMAT Statement

thanks

karthik

reward me if usefull

Read only

0 Likes
1,349

Thank you very much karthik. Rewards to you from me

Read only

Former Member
0 Likes
1,349

you need to store the external value in a variable that is declared to 4

decimal places and the use the round function to round it back to 2 places

before updating to your table