Numeric Functions in ABAP CDS View Entities

45 min read

Why Every ABAP Cloud Developer Should Master Numeric Functions

Business applications constantly perform numeric calculations. Whether you're calculating discounts, taxes, quantities, averages, percentages, exchange rates, or analytical KPIs, numeric operations are an essential part of almost every CDS View Entity.

Instead of retrieving raw data and performing calculations in ABAP, SAP recommends pushing these calculations to the database whenever possible. This approach allows SAP HANA to execute the logic directly, improving performance and reducing application server processing.

ABAP CDS provides several built-in numeric functions for rounding, division, absolute values, and mathematical calculations. Understanding when and how to use these functions is an important skill for every RAP and ABAP Cloud developer.

Numeric Functions in ABAP CDS
Numeric functions enable SAP HANA to perform mathematical calculations directly in CDS View Entities, supporting efficient code pushdown and analytical reporting.
Click image to enlarge

Learning Objectives

After completing this lesson, you'll understand the purpose, behavior, and limitations of the most commonly used numeric functions available in ABAP CDS View Entities.

After Completing This Lesson You Will Be Able To

SkillDescription
Understand Numeric FunctionsExplain the purpose of each built-in numeric function.
Round ValuesUse CEIL(), FLOOR(), and ROUND() appropriately.
Perform DivisionChoose between DIV(), DIVISION(), and MOD().
Avoid Compiler ErrorsUnderstand supported data types for each function.
Build Better CDS ViewsPush mathematical calculations to SAP HANA.

Numeric Functions Overview

SAP provides several built-in numeric functions for performing common mathematical operations. Each function is designed for a specific purpose and supports a defined set of data types.

Numeric Functions

FunctionPurposeTypical Usage
ABS()Returns absolute valueFinancial calculations
CEIL()Rounds upwardPackaging calculations
FLOOR()Rounds downwardInventory calculations
ROUND()Rounds to specified decimalsCurrency reporting
DIV()Integer divisionBusiness calculations
DIVISION()Decimal divisionRatios and percentages
MOD()Returns remainderGrouping and scheduling

Architect Perspective

Architect Insight

Although these functions appear similar to their ABAP counterparts, they execute directly in SAP HANA as part of the SQL statement. Understanding their supported data types is essential because not every numeric function accepts the same operand types.

ABS() - Absolute Value

The ABS() function returns the absolute value of a numeric expression by removing its sign.

Syntax
ABAP CDS
abs( numeric_expression )
Example
ABAP CDS
abs( TotalNetAmount ) as AbsoluteAmount

Example Results

InputOutput
100100
-100100
250.75250.75
-250.75250.75

Real Project Example: Financial Reporting

Credit memos are often stored as negative amounts, while invoices are stored as positive amounts. When preparing analytical reports, businesses sometimes want to compare transaction values regardless of their sign.

Using ABS() allows both invoices and credit memos to be displayed as positive values for reporting purposes.

CEIL() - Round Up

The CEIL() function always rounds a numeric value upward to the next whole integer.

Syntax
ABAP CDS
ceil( numeric_expression )
Example
ABAP CDS
ceil( GrossWeight ) as RoundedWeight

Example Results

InputOutput
10.0111
10.5011
10.9911
10.0010

Real Project Example: Packaging Calculation

A warehouse ships products in full cartons. If one carton can hold 10.5 kilograms, shipping 10.1 kilograms still requires one complete carton.

CEIL() ensures that fractional values are always rounded upward so enough packaging is allocated.

FLOOR() - Round Down

The FLOOR() function rounds a numeric value downward to the nearest whole integer.

Syntax
ABAP CDS
floor( numeric_expression )
Example
ABAP CDS
floor( GrossWeight ) as RoundedWeight

Example Results

InputOutput
10.9910
10.5010
10.0110
10.0010

Real Project Example: Inventory Reporting

A manufacturing report may need to display only completed units produced during a shift. Any partially completed unit should not be counted.

FLOOR() removes the fractional component and returns only the completed quantity.

💡 SAP Best Practice

Remember the difference:

CEIL() always rounds upward.
FLOOR() always rounds downward.
Neither function follows traditional mathematical rounding rules.

ROUND() - Round to a Specified Number of Decimal Places

The ROUND() function rounds a numeric value to the specified number of decimal places. Unlike CEIL() andFLOOR(), ROUND follows normal mathematical rounding rules.

Syntax
ABAP CDS
round( numeric_expression, decimal_places )
Example
ABAP CDS
round(
    TotalNetAmount,
    2
) as RoundedAmount

Example Results

InputDecimal PlacesOutput
125.4562125.46
125.4542125.45
125.50126
125.40125

Real Project Example: Financial Reporting

Currency amounts are commonly stored with greater precision during calculations but displayed with two decimal places in reports and Fiori applications.

ROUND() ensures that values are presented using the required business precision.

DIV() - Integer Division

The DIV() function performs integer division and returns only the quotient. Any fractional part of the result is discarded.

Syntax
ABAP CDS
div( operand1, operand2 )
Example
ABAP CDS
div(
    cast( TotalNetAmount as abap.dec(13,2) ),
    20
) as Quotient

Example Results

ExpressionResult
125 DIV 206
99 DIV 109
45 DIV 76

Architect Perspective

Important

Although DIV performs integer division, it supports more than just integer operands. It also accepts certain decimal business types such as DEC and QUAN.

Supported Data Types for DIV()

SupportedNot Supported
INT1DECFLOAT16
INT2DECFLOAT34
INT4
INT8
DEC
QUAN

DIVISION() - Decimal Division

While DIV() returns only the integer quotient, the DIVISION() function preserves decimal precision.

Syntax
ABAP CDS
division(
    operand1,
    operand2,
    decimal_places
)
Example
ABAP CDS
division(
    TotalNetAmount,
    20,
    2
) as AverageValue

Example Results

ExpressionResult
125 / 206.25
100 / 812.50
50 / 316.67

Real Project Example: Percentage Calculation

Financial reports often calculate ratios, percentages, and average values where decimal precision is essential.

DIVISION() is the preferred function because it preserves the fractional component instead of truncating the result.

MOD() - Remainder After Integer Division

The MOD() function returns the remainder after integer division.

Syntax
ABAP CDS
mod( operand1, operand2 )
Example
ABAP CDS
mod(
    cast( TotalNetAmount as abap.int4 ),
    20
) as Remainder

Example Results

ExpressionResult
125 MOD 205
30 MOD 42
100 MOD 84

Architect Perspective

Architect Insight

MOD() is strictly an integer arithmetic function. Unlike DIV(), it does not accept DEC, QUAN, or DECFLOAT data types.

A Common Compiler Error with MOD()

Many developers assume that DIV() and MOD() support the same operand types. This assumption leads to one of the most common activation errors in CDS development.

Incorrect Example
ABAP CDS
mod(
    cast( TotalNetAmount as abap.dec(13,2) ),
    20
) as ModAmount
Compiler Error
ABAP CDS
Function MOD:
Type DEC not supported by parameter 1.

Expected:
INT1
INT2
INT4
INT8

Although TotalNetAmount is numeric, MOD() accepts only integer data types. Therefore, the expression fails during CDS activation.

Correct Solution
ABAP CDS
mod(
    cast( TotalNetAmount as abap.int4 ),
    20
) as ModAmount

Why Doesn't MOD() Support DEC?

This restriction is intentional and often discussed in architect interviews.

Consider the following calculation:

Example
ABAP CDS
25.75 MOD 4 = ?

Should the answer be:

  • 1.75
  • 1
  • Another value?

Different databases and programming languages interpret decimal modulo operations differently. To avoid ambiguity and ensure consistent behavior, ABAP CDS restricts MOD() to integer operands.

Architect Perspective

Architect Summary

Remember this simple rule:

DIV() → Integer quotient
DIVISION() → Decimal division
MOD() → Integer remainder

DIV() vs DIVISION() vs MOD()

Comparison

FunctionPurposeAccepts DECAccepts QUANAccepts DECFLOAT
DIV()Integer Quotient
DIVISION()Decimal Division✅*
MOD()Integer Remainder

💡 SAP Best Practice

Don't assume that DIV() and MOD() support the same operand types. Although both are related to division, they serve different purposes and have different type restrictions.

Common Mistakes

Numeric functions are easy to use, but developers frequently make mistakes because they assume every numeric function supports the same data types and behaves identically.

Common Mistakes

  • Assuming DIV() and MOD() support the same operand types.
  • Using MOD() with DEC or QUAN values.
  • Using DIV() when decimal precision is required.
  • Using CEIL() or FLOOR() instead of ROUND() for financial calculations.
  • Ignoring the supported data types of individual numeric functions.
  • Performing calculations in ABAP that can be pushed down to SAP HANA.

Common Compiler Errors

The following compiler errors are frequently encountered while working with numeric functions in CDS View Entities.

Compiler Error 1
ABAP CDS
Function MOD:
Type DEC not supported by parameter 1.

Reason: MOD() accepts only integer data types (INT1, INT2,INT4, and INT8).

Incorrect
ABAP CDS
mod(
    cast( TotalNetAmount as abap.dec(13,2) ),
    20
)
Correct
ABAP CDS
mod(
    cast( TotalNetAmount as abap.int4 ),
    20
)
Compiler Error 2
ABAP CDS
Function DIV:
Type DECFLOAT34 not supported.

Reason: DIV() supports integer, DEC, and QUAN data types but does not support DECFLOAT operands.

Architect Perspective

Architect Insight

Always verify the supported operand types in the CDS documentation or ADT code completion before assuming two numeric functions behave the same way.

Choosing the Right Numeric Function

Function Selection Guide

RequirementRecommended Function
Remove negative signABS()
Round upwardCEIL()
Round downwardFLOOR()
Normal mathematical roundingROUND()
Integer quotientDIV()
Decimal divisionDIVISION()
Integer remainderMOD()

💡 SAP Best Practice

Choose the function based on the required business result rather than simply selecting the first function that compiles.

Performance Considerations

Numeric functions are executed directly in SAP HANA as part of the generated SQL statement. This allows calculations to benefit from code pushdown and minimizes application server processing.

Performance Recommendations

RecommendationReason
Perform calculations in CDS whenever possible.Reduces ABAP processing.
Avoid duplicate calculations.Improves maintainability.
Use DIVISION() when decimal precision matters.Prevents unnecessary post-processing.
Keep expressions readable.Makes CDS Views easier to maintain.

Interview Questions

BeginnerInterview Question

What is the difference between CEIL() and FLOOR()?

Answer: CEIL() always rounds upward to the next integer, whereas FLOOR() always rounds downward to the previous integer.

BeginnerInterview Question

Which function returns the absolute value of a number?

Answer: ABS() removes the sign and returns the absolute value.

ExperiencedInterview Question

What is the difference between DIV() and DIVISION()?

Answer: DIV() returns only the integer quotient, whereas DIVISION() performs decimal division and preserves the fractional part.

ExperiencedInterview Question

Why does MOD() reject DEC values?

Answer: MOD() is defined as an integer arithmetic function. To avoid ambiguity in decimal remainder calculations, ABAP CDS restricts it to integer operands.

ExperiencedInterview Question

Can DIV() and MOD() be used interchangeably?

Answer: No. Although both relate to division, DIV() returns the quotient while MOD() returns the remainder, and they support different operand types.

ExperiencedInterview Question

When would you choose DIVISION() over DIV()?

Answer: Whenever decimal precision is required, such as ratios, percentages, averages, or financial calculations.

ArchitectInterview Question

Why are numeric functions recommended in CDS instead of ABAP?

Answer: They execute directly in SAP HANA, enabling code pushdown, reducing data transfer, and improving performance.

ArchitectInterview Question

What is one common misconception about MOD()?

Answer: Many developers assume MOD() supports DEC and QUAN because DIV() does. In reality, MOD() supports only integer data types.

🧮
Developer Reference

Numeric Functions Cheat Sheet

7 Functions • Mathematical Operations • SAP HANA Pushdown

This quick reference summarizes the most commonly used numeric functions in ABAP CDS. Keep it nearby while performing calculations, financial processing, analytical reporting, or preparing for technical interviews.

FunctionSyntaxPurposeReturns
ABS()abs(number)Returns the absolute valueNumeric
CEIL()ceil(number)Rounds upward to the next integerInteger
FLOOR()floor(number)Rounds downward to the previous integerInteger
ROUND()round(number, decimals)Rounds to the specified decimal placesRounded Numeric
DIV()div(dividend, divisor)Returns the integer quotientInteger
DIVISION()division(dividend, divisor, scale)Performs decimal division with precisionDecimal
MOD()mod(dividend, divisor)Returns the integer remainderInteger

💡 Key Takeaway

Numeric functions allow SAP HANA to perform mathematical calculations directly inside CDS View Entities, reducing the need for ABAP post-processing and improving application performance.

Understanding the differences between DIV(), DIVISION(), and MOD() is particularly important because they solve different mathematical problems and support different operand types.

Always choose the function that best matches the business requirement, verify the supported data types, and push calculations to the database whenever possible to build clean and production-ready RAP applications.

Watch the Complete ADT Walkthrough

In the accompanying video, we'll implement every numeric function discussed in this lesson using Eclipse ADT and SAP S/4HANA Public Cloud. We'll also reproduce common compiler errors, explain why they occur, and demonstrate the correct production-ready solutions.