  ## Class BirtMath

The BirtMath class provides functions to manipulate numeric data, for example, to add, subtract, multiple, divide, and round numbers. This class is static. The application cannot create instances of the class.

This function returns the sum of two numbers.

Syntax

BirtMath.add( n1, n2 )

Parameters

n1

The first number to use in the calculation.

n2

The second number to use in the calculation.

Returns

A number that results from adding n1 and n2.

Example

BirtMath.add( row["SalesTotal"], row["Shipping"] )

### BirtMath.ceiling

This function rounds a number up, away from 0, to the nearest specified multiple.

Syntax

BirtMath.ceiling( n, significance )

Parameters

n

The number to round up.

significance

The multiple to round n to.

Returns

Examples

BirtMath.ceiling( ) is commonly used to round up prices. For example, to avoid dealing with pennies, you can round prices in a Price field up to the nearest nickel with the following expression:

BirtMath.ceiling( row["Price"] , 0.05 )

If the Price value is 20.52, the expression returns 20.55.

The following expression rounds prices up to the nearest dime:

BirtMath.ceiling( row["Price"] , 0.1 )

If the Price value is 20.52, the expression returns 20.60. If the Price value is 20.40, the expression returns 20.40. No rounding occurs because 20.40 is a multiple of 0.1.

The following expression rounds prices up to the nearest dollar:

BirtMath.ceiling( row["Price"] , 1 )

If the Price value is 20.30, the expression returns 21.

### BirtMath.divide

This function returns the result of dividing one number by another.

Syntax

BirtMath.divide( dividend, divisor )

Parameters

dividend

The number to be divided.

divisor

The number by which dividend is divided.

Returns

A number that results from dividing dividend by divisor.

Examples

The following example shows the results of dividing specific numbers:

BirtMath.divide( 10, 2 ) // returns 5
BirtMath.divide( 2, 10) // returns 0.2

The following example returns the result of dividing values in the Total field by values in the Quantity field:

BirtMath.divide( row["Total"], row["Quantity"] )

### BirtMath.mod

This function returns the remainder after a number is divided by a divisor.

Syntax

BirtMath.mod( number, divisor )

Parameters

number

The number to be divided.

divisor

Returns

The remainder after number is divided by divisor.

Examples

BirtMath.mod( 10, 3 ) // returns 1
BirtMath.mod( 10, 5 ) // returns 0
BirtMath.mod( 10, 6 ) // returns 4

The following example checks if numbers in the Grade field are odd or even. When the divisor is 2, the function returns 0 for even numbers, and 1 for odd numbers.

BirtMath.mod( row["Grade"], 2 )

### BirtMath.multiply

This function returns the multiplication of two numbers.

Syntax

BirtMath.multiply( n1, n2 )

Parameters

n1

The first number to use in the calculation.

n2

The second number to use in the calculation.

Returns

A number that results from multiplying n1 and n2.

Example

The following example returns the result of multiplying values in the QuantityOrdered and PriceEach fields:

BirtMath.multiply( row["QuantityOrdered"], row["PriceEach"] )

### BirtMath.round

This function rounds a number to a specified number of digits.

Syntax

BirtMath.round( number, dec )

Parameters

number

The number to round.

dec

Returns

A number rounded to a specified number of digits.

Examples

BirtMath.round( 1545.50 ) // returns 1546
BirtMath.round( 1545.56, 1 ) // returns 1545.6
BirtMath.round( 1545.23, 1 ) // returns 1545.2
BirtMath.round( 1545.50, -1 ) // returns 1550
BirtMath.round( row["PriceEstimate"], 0 )

### BirtMath.roundDown

This function rounds a number down to a specified number of digits.

Syntax

BirtMath.roundDown( number, dec )

Parameters

number

The number to round.

dec

The number of digits to round number down to. If you omit this argument, the function assumes 0.

Returns

A number rounded down to a specified number of digits.

Examples

BirtMath.roundDown( 1545.50 ) // returns 1545
BirtMath.roundDown( 1545.56, 1 ) // returns 1545.5
BirtMath.roundDown( 1545.23, 1 ) // returns 1545.2
BirtMath.roundDown( 1545.50, -1 ) // returns 1540
BirtMath.roundDown( row["PriceEstimate"], 0 )

### BirtMath.roundUp

This function rounds a number up to a specified number of digits.

Syntax

BirtMath.roundUp( number, dec )

Parameters

number

The number to round up.

dec

The number of digits to round number up to. If you omit this argument, the function assumes 0.

Returns

A number rounded up to a specified number of digits.

Examples

BirtMath.roundUp( 1545.50 ) // returns 1546
BirtMath.roundUp( 1545.56, 1 ) // returns 1545.6
BirtMath.roundUp( 1545.23, 1 ) // returns 1545.3
BirtMath.roundUp( 1545.50, -1 ) // returns 1550
BirtMath.roundUp( row["PriceEstimate"], 0 )

### BirtMath.safeDivide

This function returns the result of dividing one number by another, preventing a division by zero condition.

Syntax

BirtMath.safeDivide( dividend, divisor, ifZero )

Parameters

dividend

The number to be divided.

divisor

The number by which dividend is divided.

ifZero

The value to return when divisor is zero.

Returns

A number that results from dividing dividend by divisor.

Examples

BirtMath.safeDivide( 10, 2, 0 ) // returns 5
BirtMath.safeDivide( 10, 0, 0 ) // returns 0

The following example returns the result of dividing values in the Revenue field by values in the Volume field. If the Volume value is 0, the function returns 0.

BirtMath.safeDivide( row["Revenue"], row["Volume"], 0 )

### BirtMath.subtract

This function returns the result of subtracting one number from another.

Syntax

BirtMath.subtract( n1, n2 )

Parameters

n1

The number from which to subtract.

n2

The number to subtract from n1.

Returns

A number that results from subtracting n2 from n1.

Example

The following example shows the results of subtracting specific numbers:

BirtMath.subtract( 12, 4 ) // returns 8
BirtMath.subtract( 4, 12) // returns -8

The following example returns the result of subtracting values in the Discount field from values in the Total field:

BirtMath.subtract( row["Total"], row["Discount"] )