SQL Server administration and T-SQL development, Web Programming with ASP.NET, HTML5 and Javascript, Windows Phone 8 app development, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software SAP ABAP Programming and HANA Database Tutorials
Development resources, articles, tutorials, samples, codes and tools for .Net, SQL Server, Windows, Windows Phone, SAP and ABAP, like SAP UI5, Screen Personas, etc.




CRM Companies List
Web Based CRM Software


Read Integer and Decimals of a Numeric Value in ABAP Programming

In ABAP programming, to read integer part of a numeric value and to read decimal part of a numeric value is very easy using numeric ABAP function TRUNC and FRAC function.

This ABAP tutorial will demonstrate how to read decimals of a given floating number using ABAP FRAC function. As well as reading decimals, developers will see how to read integer part of numeric values using ABAP TRUNC function. As one last step I'll concatenate integer part and decimals for a specific country using that locale's thousand separator and decimal separator character.

Read Integers using ABAP TRUNC Function

Here is a sample case where ABAP developers can use TRUNC function to read integer part of numeric values in SAP systems. This ABAP example reads integer part of invoice items net and gross weight information from ABAP VBRP table BRGEW gross weight field and NTGEW net weight field for a given invoice number.

DATA :
 lv_vbeln TYPE vbeln,
 lwa_vbrp TYPE vbrp,
 lv_ntgew TYPE i,
 lv_brgew TYPE i,
 gv_net_weight(20) TYPE c,
 gv_gross_weight(20) TYPE c.

lv_vbeln = '0030000184'. " Sample invoice number

SELECT SINGLE * INTO lwa_vbrp FROM vbrp WHERE vbeln = lv_vbeln.

IF sy-subrc EQ 0.
 lv_ntgew = TRUNC( lwa_vbrp-ntgew ). " Read integer part
 lv_brgew = TRUNC( lwa_vbrp-brgew ). " Read integer part

 WRITE lv_ntgew TO gv_net_weight DECIMALS 0 NO-GROUPING. " Write as text
 WRITE lv_brgew TO gv_gross_weight DECIMALS 0 NO-GROUPING.

 CONDENSE gv_net_weight. " Remove spaces
 CONDENSE gv_gross_weight. " Remove spaces

 WRITE gv_net_weight. " Write on display
 WRITE gv_gross_weight. " Write on display
ENDIF.

read integer part of a numberic value in ABAP programming

This ABAP report will display 1887 as net weight and 2026 as gross weight value ignoring the decimals and only showing integer part of the item's weight data ntgew and brgew field values.


Read Decimals using ABAP FRAC Function

ABAP programmers sometimes require to read decimals of numeric values for example for custom rounding in ABAP calculations. This ABAP program example will show how to read decimals of invoice item's weight data using ABAP FRAC function.

DATA :
 lv_vbeln TYPE vbeln,
 lwa_vbrp TYPE vbrp,
 lv_ntgew TYPE i,
 gv_netweightdec(10) TYPE c.

lv_vbeln = '0030000184'. " Sample invoice number

SELECT SINGLE * INTO lwa_vbrp FROM vbrp WHERE vbeln = lv_vbeln.

IF sy-subrc EQ 0.
 lv_ntgew = FRAC( lwa_vbrp-ntgew ).
 MOVE lv_ntgew TO gv_netweightdec.
 CONDENSE gv_netweightdec.
 MOVE gv_netweightdec+2(3) TO gv_netweightdec.
 WRITE gv_netweightdec.
ENDIF.

ABAP FRAC function to read decimals in ABAP programming

Given above ABAP program will display 480 as decimal section of net weight of invoice item. If you use FRAC function for custom rounding, you can compare lv_ntgew with 0.500 for example within your ABAP report. If it is bigger or equal to limit value, you can increase the integer part by one. If it is less than limit value, you can just use the integer part of the numeric value for rounding off.


Convert Numeric Value using Custom Thousands and Decimal Separator

After you have splitted integer value and decimal value of the floating numeric number in your ABAP report, developers can concatenate these two values using custom thousands and decimals separator. This text representation of numeric values might be a problem for different locales and especially if you are creating Microsoft Excel reports within your ABAP program.

In English thousand separator is "," and decimal separator is "."
For example, in Turkish the case is just the opposite. Thousands separator is "." and the decimal separator is "," in Turkish numeric formatting.

Here is a sample ABAP report to split numeric value into its integer and decimals part and concatenate back using custom separators.

Within the fn_convertTRnumericDisplay ABAP function, you can define your custom thousands and decimal separator as you wish for string representation of the numeric values.

DATA :
 lv_vbeln TYPE vbeln,
 lwa_vbrp TYPE vbrp,
 gv_netweight(20) TYPE c.

lv_vbeln = '0030000184'.

SELECT SINGLE * INTO lwa_vbrp FROM vbrp WHERE vbeln = lv_vbeln.

IF sy-subrc EQ 0.
 PERFORM fn_convertTRnumericDisplay USING lwa_vbrp-ntgew CHANGING gv_netweight.
 WRITE gv_netweight.

ENDIF.


FORM fn_convertTRnumericDisplay
 USING lv_brgew LIKE vbrp-brgew
  CHANGING lv_display.

DATA :
 gv_decimalfrc LIKE vbrp-brgew,
 gv_decimaltxt(10) TYPE c,
 gv_integerpart TYPE i,
 gv_integertxt(13) TYPE n.

 gv_integerpart = TRUNC( lv_brgew ).
 WRITE gv_integerpart TO gv_integertxt DECIMALS 0. "no-GROUPING.
 CONDENSE gv_integertxt.
 REPLACE ALL OCCURRENCES OF '.' IN gv_integertxt WITH '.'. " Thousand separator
 REPLACE ALL OCCURRENCES OF ',' IN gv_integertxt WITH '.'. " Thousand separator

 gv_decimalfrc = FRAC( lv_brgew ).
 MOVE gv_decimalfrc TO gv_decimaltxt.
 CONDENSE gv_decimaltxt.
 MOVE gv_decimaltxt+2(3) TO gv_decimaltxt.

 CONCATENATE gv_integertxt ',' gv_decimaltxt INTO lv_display. " Decimal separator

ENDFORM.





SAP Tutorials

SAP Tutorial

SAP Forums

SAP Tools

SAP Transaction Codes Table






Related Articles and Tutorials(http://www.kodyaz.com/sap-abap/how-to-read-integer-and-decimals-of-numeric-values.aspx)
How to Debug Popup Screen in ABAP
ABAP LOOP Statement with At New and At End
How to Upload Data to SAP from Excel File using alsm_excel_to_internal_table Function Module
ABAP - Create Hierarchy Tree List using rs_tree_construct, rs_tree_list_display and snodetext
ABAP Tutorial - ABAP String Split Example Code
Upload Data from Excel File in ABAP using TEXT_CONVERT_XLS_TO_SAP
Create CL_GUI_ALV_GRID ALV Grid Column Header using ABAP Data Element
How to Create SAPScript Text using SO10 Transaction Code
ALV Grid Color - Table Row Background Color in ALV List
SAP ABAP Tutorial - ALV Grid Example with cl_gui_alv_grid and Screen Painter
SAP Smartforms - How to Call Smartform within ABAP Program
ABAP Tutorial - SAP Split and ABAP Split String Function
SAP Tutorial - Convert Spool Request to PDF File using RSTXPDFT4 ABAP Report
Set Default Single Values List for SAP Selection Screen Parameter
ABAP Debugging using Log Point and Break Point
ABAP RegEx for ABAP Regular Expression to fetch Date from String
ABAP Runtime Error ST22 Transaction using Short Dump Analysis
ABAP Shared Memory Objects Tutorial with Sample ABAP Code
ABAP Try Catch for exception is neither caught Code Inspector Error
Add Year, Month or Day to Date Variable in ABAP using RP_CALC_DATE_IN_INTERVAL
Read Address Data using FM ADDR_GET instead of Querying ADRC Table
ABAP Authority-Check Object For User Other Than Current
SAP Code Inspector for ABAP Break-Point Statement
Call Function Module using RFC Destination in ABAP
CALL_FUNCTION_NOT_FOUND ABAP Run Time Error
Check Existence using ABAP SELECT
How to Spell Numbers using ABAP Spell_Amount Function Module
Convert the table using the Database Utility (SE14)
Convert Sales Unit into Text using Function Module in Target Language
Create Business Object in SAP
Delete All Data from SAP Database Table
Create SAP Dynamic Variant using Date Calculation
Create SAP Transaction to Maintain ABAP Table using SM30
Database Hint Pseudo-Comment for SAP Code Inspector
Delete Entries in NAST Table using RSCLNAST ABAP Program
Dynamic Programming using Field Symbols in ABAP Reports
Prevent DYNPRO_SEND_IN_BACKGROUND Error in Background Process
Execute ABAP Program Automatically After Transport Request Import
How to Execute SAP Program in Background Task
How to Find Internal Table Row Count in ABAP
Export SAP Data to Fixed Length Text File
Get Fullname of SAP user using Function Module in ABAP Program
How to Find User Exits in ABAP Codes of SAP Transaction
Read Integer and Decimals of a Numeric Value in ABAP Programming
Move Minus Sign from Right to Left of Negative Number in SAP Smartform
Primary key change not permitted for value table
Read Pricing Condition Text using ABAP in SAP Sales Document Output
Read Text Symbols in Specific Language using ABAP RS_TEXTPOOL_READ
Get SAP Document Flow using SD_DOCUMENT_FLOW_GET ABAP Function Module
Export Table Contents into Text File and Download File
Search and Find SAP Transaction Code using SE93 or SEARCH_SAP_MENU
SAP Code Inspector Error: Select For Cluster Table Without Order By
ABAP Select Statement using Space instead of Null or Initial
SAP Code Inspector: Select-Statement can be Transformed
ABAP Tutorial for SET and GET PARAMETER ID Command
ABAP CONVT_NO_NUMBER Error on SP01 due SP01_WARN Parameter
How to find string length using STRLEN ABAP string function
ABAP Submit Report to Run Another ABAP Program within Code
Transport Variants of an ABAP Program into Workbench Request
TVAP Item Category and SAP Table TVAPT for Text Translations
Code Inspector: Unsecure use of FOR ALL ENTRIES
Update Multiple Variables in ABAP Select Single Statement
Function module is not released for the Internet
Activate Fixed Point Arithmetic Flag for Open SQL Statement
Activate Fixed Point Arithmetic Flag for Open SQL Statement
Activate Fixed Point Arithmetic Flag for Open SQL Statement
Activate Fixed Point Arithmetic Flag for Open SQL Statement
Activate Fixed Point Arithmetic Flag for Open SQL Statement
Activate Fixed Point Arithmetic Flag for Open SQL Statement
Activate Fixed Point Arithmetic Flag for Open SQL Statement
Activate Fixed Point Arithmetic Flag for Open SQL Statement
Activate Fixed Point Arithmetic Flag for Open SQL Statement
Activate Fixed Point Arithmetic Flag for Open SQL Statement
Activate Fixed Point Arithmetic Flag for Open SQL Statement
Activate Fixed Point Arithmetic Flag for Open SQL Statement


Copyright © 2004 - 2017 Eralper YILMAZ. All rights reserved.
Community Server by Telligent Systems