Usina BRAdvPL Guide
Sign in
← All topics
FUNCTIONPublished

Abs

Abs( <nExp> )

Returns the absolute value of a number.

NumericMathCalculationTDN
01 · OVERVIEW

Overview

Returns the absolute value of a number. This page was created from the historical training material and structured for quick reference, with practical notes and cautious use in current Protheus environments.

02 · SYNTAX

Syntax

Abs( <nExp> )

Parameters

nExp
NumericRequired

Numeric expression evaluated to obtain its absolute value.

Return value

Returns nRet, numeric, representing the absolute value of the supplied expression. The result is positive or zero.

03 · PRACTICAL EXAMPLE

Absolute difference

User Function ExAbs()
    Local nSeats := 12
    Local nPeople := 15
    Local nDifference := Abs(nSeats - nPeople)

    MsgInfo("Difference: " + CValToChar(nDifference))
Return
Expected result

Even when nSeats - nPeople returns a negative number, Abs() returns the positive difference.

04 · PRACTICAL EXAMPLE

Absolute value of a negative number

User Function Abs2()
    Local nValue := -123.45

    MsgInfo( Abs( nValue ) ) // Result: 123.45
Return
Expected result

The function removes the negative sign and displays 123.45.

BEST PRACTICES
  • Use quando o sinal não importar para a regra.
  • É uma forma simples de obter a diferença positiva entre dois números sem saber previamente qual deles é maior.
  • Pela definição matemática, se x for maior ou igual a zero o retorno é x; se x for menor que zero o retorno é -x.
  • A referência TDN informa abrangência histórica em Advanced Protheus 6.09, Advanced Protheus 7.10, Microsiga Protheus 8.11 e Protheus 10.
COMMON PITFALLS
  • Do not use Abs() to hide an improper accounting difference; handle the source of the sign when it has meaning.
  • Validate null or nonnumeric values before calculating.
  • In financial rules, confirm that converting negative values to positive does not change the meaning of credit, debit, inflow or outflow.

Related content

REFERENCES
  1. TOTVS. Abs. TDN Framework. Returns the absolute value of a numeric expression.
  2. Programação em ADVPL Completo. Training material recorded in the official documentation, section 4.5.4, p. 45-50. Summarized, reviewed and examples adapted.
Status
Published
Page created on
Last reviewed on
Original language
Portuguese
Reviewed by
Usina.BR
0 approved comment(s)

Comments

There are no approved comments yet.

Sign in with Google or Microsoft to comment.

Powered by Usina Docs · Alpha