Wednesday, 22 October 2014

ARTHEMETIC OPERATIONS SAPABAP

Arithmetic Calculations 
Arithmetic calculations use arithmetic operations and special ABAP statements.
Basic Arithmetic Operations
ABAP supports the four basic arithmetic operations and power calculations. You can use the following arithmetic operators in mathematical expressions:
Operator
Meaning
+
Addition
-
Subtraction
*
Multiplication
/
Division
DIV
Integer division
MOD
Remainder of integer division
**
Powers
Instead of using operators in mathematical expressions, you can perform basic arithmetic operations with the keywords ADD, SUBTRACT, MULTIPLY, and DIVIDE.
The following table shows the different ways of expressing basic arithmetic operations in ABAP:
Operation
Statement using
mathematical expression
Statement using
Keyword
Addition
<p> = <n> + <m>.
ADD <n> TO <m>.
Subtraction
<p> = <m> - <n>.
SUBTRACT <n> FROM <m>.
Multiplication
<p> = <m> * <n>.
MULTIPLY <m> BY <n>.
Division
<p> = <m> / <n>.
DIVIDE <m> BY <n>.
Integer division
<p> = <m> DIV <n>.
---
Remainder of division
<p> = <m> MOD <n>.
---
Powers
<p> = <m> ** <n>.
---
In the statements using keywords, the results of the operations are assigned to field <m>.
The operands <m>, <n>, <p> can be any numeric fields. If the fields are not of the same data type, the system converts all fields into the hierarchically highest data type that occurs in the statement.
When using mathematical expressions, please note that the operators +, -, *, **, and /, as well as opening and closing parentheses, are interpreted as words in ABAP and must therefore be preceded and followed by blanks.
In division operations, the divisor cannot be zero if the dividend is not zero. With integer division, you use the operators DIV or MOD instead of /. DIV calculates the integer quotient, MOD calculates the remainder.
When you combine arithmetic expressions, ABAP performs the calculation from left to right, with one exception: Powers are calculates from right to left. So <n> ** <m> ** <p> is the same as <n> ** ( <m> ** <p> ), not ( <n> ** <m> ) ** <p>.
DATA: COUNTER TYPE I.
COMPUTE COUNTER = COUNTER + 1.
COUNTER = COUNTER + 1.
ADD 1 TO COUNTER.
Here, the three operational statements perform the same arithmetic operation, i.e. adding 1 to the contents of the field COUNTER and assigning the result to COUNTER.
DATA: PACK TYPE P DECIMALS 4,
N TYPE F VALUE '+5.2',
M TYPE F VALUE '+1.1'.
PACK = N / M.
WRITE PACK.
PACK = N DIV M.
WRITE / PACK.
PACK = N MOD M.
WRITE /PACK.
The output appears as follows:
  4.7273
  4.0000
  0.8000
This example shows the different types of division.
Arithmetic Calculations Using Structures
In the same way that you can transfer values component by component between structures using the
MOVE-CORRESPONDING statement, you can also perform arithmetic operations between the components of structures using the following statements:
  • ADD-CORRESPONDING
  • SUBTRACT-CORRESPONDING
  • MULTIPLY-CORRESPONDING
  • DIVIDE-CORRESPONDING
ABAP performs the corresponding calculation for all components that have the same name in both structures. However, it only makes sense to use the operations if all of the components involved have a numeric data type.
DATA: BEGIN OF RATE,
USA TYPE F VALUE '0.6667',
FRG TYPE F VALUE '1.0',
AUT TYPE F VALUE '7.0',
END OF RATE.
DATA: BEGIN OF MONEY,
USA TYPE I VALUE 100,
FRG TYPE I VALUE 200,
AUT TYPE I VALUE 300,
END OF MONEY.
MULTIPLY-CORRESPONDING MONEY BY RATE.
WRITE / MONEY-USA.
WRITE / MONEY-FRG.
WRITE / MONEY-AUT.
The output appears as follows:
 67
  200
  2,100
Here, MONEY-USA is multiplied by RATE-USA and so on.
Adding Sequences of Fields
There are variants of the ADD statement that allow you to add sequences of fields in memory. For example:
Adding sequences of fields and assigning the result to another field
ADD <n1> THEN <n 2> UNTIL <n z> GIVING <m>.
If <n
1 >, <n 2 >, ... , <n z > is a sequence of equidistant fields of the same type and length in memory, they are summed and the result is assigned to <m>.
Adding sequences of fields and adding the result to the contents of another field
ADD <n1> THEN <n 2> UNTIL <n z> TO <m>.
This statement is identical to the preceding one, with one difference: The sum of the fields is added to the existing contents of <m>.
For further information about other similar variants, see the keyword documentation for the ADD statement.

DATA: BEGIN OF SERIES,
N1 TYPE I VALUE 10,
N2 TYPE I VALUE 20,
N3 TYPE I VALUE 30,
N4 TYPE I VALUE 40,
N5 TYPE I VALUE 50,
N6 TYPE I VALUE 60,
END OF SERIES.
DATA SUM TYPE I.
ADD SERIES-N1 THEN SERIES-N2 UNTIL SERIES-N5 GIVING SUM.
WRITE SUM.
ADD SERIES-N2 THEN SERIES-N3 UNTIL SERIES-N6 TO SUM.
WRITE / SUM.
Output
  150
  350
Here, the contents of components N1 to N5 are summed and assigned to the field SUM. Then, the contents of components N2 to N6 are summed and added to the value of SUM.

SUPPRESS A SIGN BEFORE A NUMBER


hi,

use

data : a type p decimals 2 value '13245.56-',
       b type p decimals 2.

       b = abs( a ).
       write : b.

BEST SITE FOR SAP_ABAP INTERVIEW

SYSTEM VARIABLES OF SAP_ABAP

Skip to end of metadataSystem fields for SCREENS.
System fields are set at the PAI (Process After Input) event for every screen.
These can be also used for interactive list processing, except for SY-DATAR, SY-LOOPC, and SY-STEPL.
SY-CUCOL
Horizontal cursor position Counting starts at column 2
SY-CUROW
Vertical cursor position Counting starts at row 1
SY-DATAR
Set to X at time of PAI if at least one screen input field was changed by the user or other data transfer, otherwise empty.
SY-LOOPC
Number of rows currently displayed in a screen table (table control).
SY-SCOLS
Number of columns in current screen.
SY-SROWS
Number of rows in current screen.
SY-STEPL
Index of current row in a screen table (table control). Is set at every loop pass. Outside a loop, for example during the POV(Process On Value Request) event for a table row, SY-STEPL is not set appropriately.
SY-TITLE
Text that appears in the title bar of the screen. For selection screens and lists this is the program name, otherwise SAP R/3. Can be set during PBO (Process Before Output) using SET TITLEBAR.
SY-UCOMM
Function code that triggered the event PAI. Every user action that triggers PAI is assigned a unique function code, with one exception: Choosing Enter triggers PAI and different function codes can be transferred to SY-UCOMM:
• If there is an entry in the command field of the standard toolbar, this is transferred to SY-UCOMM as the function code.
• If there is no entry in the command field and a function code is assigned to the ENTER key, this function code is transferred to SY-UCOMM.
• If the command field does not contain an entry and no function code is assigned to the ENTER key, it is empty and the content of SY-UCOMM is not affected.
Selection Screens
SY-SLSET
Variant used to fill the selection screen.

System Fields.

SY-SUBRC       : RETURN VALUE AFTER ABAP STATEMENTS
SY-INDEX        : LOOPS,CURRENT LOOP PASS
SY-TABIX        : INTERNAL TABLE,CURRENT LINE INDEX
SY-TFILL         : INTERNAL TABLE,CURRENT NO. OF LINES
SY-LILLI          : LIST PROCESSING,CURRENT LIST LINE
SY-LSIND        : LIST PROCESSING , DETAILS LIST INDEX
SY-DBCNT       : DB OPERATIONS,NO. OF TABLE LINES PROCESSED
SY-CPROG      : ABAP PROGRAM,CALLER IN EXTERNAL PROCEDURES
SY-DATUM     : DISPLAYS CURRENT DATE
SY-DYNNR      : ABAP PROGRAM,NO. OF CURRENT SCREEN
SY-TLENG       : LINE LENGTH
SY-STEPL       : LOOP INFORMATION IN TABLE CONTROL
SY-LOOPC      : LOOP INFORMATION IN TABLE CONTROL
SY-FDPOS      : CONTAINS OFF SETS FOR THE FOUND STRING
SY-DBSYS      : Database system
SY-DYNGR : Screen group of current screen
SY-DYNNR :  Number of current screen
SY-MSGID : Message ID
SY-MSGNO : Message number
SY-MSGTY : Message type (E,I.W,...)
SY-MSGV1 : Message variable
SY-MSGV2 : Message variable
SY-MSGV3 : Message variable
SY-MSGV4 : Message variable
SY-PAGNO : Runtime: Current page in list
SY-PFKEY :  Current GUI Status
SY-COLNO :  Current List Column
SY-LINCT :  Page Length of List
SY-LINNO :  Current Line in List
SY-LINSZ :  Line width of list
SY-MACOL :  Number of Columns on Left Margin of Print List
SY-BINPT :  Program Running Under Batch Input
SY-MODNO :  Index of External Session

System Fields for Date and Time.

The following system fields are always set automatically. If necessary, the GET TIME statement synchronizes the application server time with that of the database server and writes it to the system field SY-UZEIT. SY-DATUM and the system fields for the local time zone, that is SY-TIMLO, SY-DATLO, and SY-ZONLO are also reset.
SY-DATLO
Local date of user, for example 19981129, 19990628.
SY-DATUM
Current (application server) date, for example 19981130, 19990627.
SY-DAYST
During daylight saving time X, otherwise empty.
SY-FDAYW
Factory calendar weekday, Sunday 0 ... Saturday 6.
SY-TIMLO
Local time of user, for example 154353, 225312.
SY-TZONE
Time difference to Greenwich Mean Time (UTC) in seconds, for example 3600, 10800.
SY-UZEIT
Current (application server) time, for example 164353, 215312.
SY-ZONLO
Time zone of user, for example CET, UTC.

System Fields in Lists

SY-LSIND     :Index of the list for current event for basic list = 0.
SY-LISTI      :Index of the list level from which the event was triggered
SY-LILLI       :Absolute number of the line from which the event was triggered
SY-LISEL      :Contents of the line from which the event was triggered
SY-CUROW  :Position of the line in the window from which the event was triggered (counting starts with 1)
SY-CUCOL    :Position of the column in the window from which the event was triggered (counting starts with 2)
SY-CPAGE    :Page number of the first displayed page of the list from which the event was triggered
SY-STARO   :Number of the first line of the first page displayed of the list from which the event was triggered (counting starts with 1). This line may contain the page header.
SY-STACO   :Number of the first column displayed in the list from which the event was triggered (counting starts with 1)
SY-UCOMM :Function code that triggered the event
SY-PFKEY    :Status of the list currently being displayed.