上一页 下一页

BirtMath

BirtMath 类提供了一些函数来处理数字数据,例如,对数字进行加、减、乘、除和舍入。数字是可以强制类型转换为“编辑数据绑定”对话框的“数据类型”字段中的另一种类型的通用对象。此类是静态类。应用程序无法创建此类的实例。

BirtMath.add

此函数将返回两个数的总和。

语法

Number BirtMath.add( Number n1, Number n2 )

参数

n1

Number 对象。要在计算中使用的第一个数。

n2

要在计算中使用的第二个数。

返回

Number 对象。将 n1 和 n2 相加所得到的数。

示例

以下示例将返回 SalesTotal 和 Shipping 这两个字段中的值的总和:

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

BirtMath.ceiling

此函数会将一个数向上舍入(大于 0)为最接近的指定倍数。

语法

Number BirtMath.ceiling( Number n, Number significance )

参数

n

Number 对象。要向上舍入的数。

significance

Number 对象。要将 n 舍入到的倍数。

返回

Number 对象。通过舍入所得到的数。如果 n 刚好是 significance 的倍数,那么将不会进行舍入。

示例

BirtMath.ceiling( ) 通常用来将价格向上舍入。例如,为了避免处理便士,可以通过以下表达式将 Price 字段中的价格向上舍入为最接近的五分镍币:

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

如果“价格”值为 20.52,那么此表达式将返回 20.55。

以下表达式会将价格向上舍入为最接近的 10 分铸币:

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

如果“价格”值为 20.52,那么此表达式将返回 20.60。如果“价格”值为 20.40,那么此表达式将返回 20.40。因为 20.40 刚好是 0.1 的倍数,所以不会进行舍入。

以下表达式会将价格向上舍入为最接近的美元:

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

如果“价格”值为 20.30,那么此表达式将返回 21。

BirtMath.divide

此函数将返回用一个数除以另一个数所得到的结果。

语法

Number BirtMath.divide( Number dividend, Number divisor )

参数

dividend

Number 对象。被除数。

divisor

Number 对象。除数。

返回

Number 对象。将被除数除以除数所得到的数。

示例

以下示例将显示除以特定数所得到的结果:

BirtMath.divide( 10, 2 ) // 将返回 5
BirtMath.divide( 2, 10) // 将返回 0.2

以下示例将返回用 Total 字段中的值除以 Quantity 字段中的值所得到的结果:

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

BirtMath.mod

此函数将返回用一个数除以除数之后所得到的余数。

语法

Number BirtMath.mod( Number number, Number divisor )

参数

number

Number 对象。被除数。

divisor

Number 对象。将第一个数作为被除数的数。您必须指定一个非零数。

返回

将一个数除以除数之后所得到的余数。

示例

下列示例将显示此函数将为特定数返回的结果

BirtMath.mod( 10, 3 ) // 将返回 1
BirtMath.mod( 10, 5 ) // 将返回 0
BirtMath.mod( 10, 6 ) // 将返回 4

以下示例将检查 Grade 字段中的数是奇数还是偶数。当除数为 2 时,此函数将对偶数返回 0,对奇数返回 1。

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

BirtMath.multiply

此函数将返回两个数的乘积。

语法

Number BirtMath.multiply( Number n1, Number n2 )

参数

n1

Number 对象。要在计算中使用的第一个数。

n2

Number 对象。要在计算中使用的第二个数。

返回

Number 对象。将 n1 和 n2 相乘所得到的数。

示例

以下示例将返回 QuantityOrdered 和 PriceEach 这两个字段中的值相乘所得到的结果:

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

BirtMath.round

此函数会将一个数舍入为指定的位数。

语法

Number BirtMath.round( Number number, Number dec )

参数

number

Number 对象。要舍入的数。

dec

Number 对象。要将一个数舍入为的位数。如果省略此自变量,那么此函数将假定值为 0。

返回

Number 对象。舍入为指定位数的数。

示例

下列示例将显示此函数将为特定数返回的结果

BirtMath.round( 1545.50 ) // 将返回 1546
BirtMath.round( 1545.56, 1 ) // 将返回 1545.6
BirtMath.round( 1545.23, 1 ) // 将返回 1545.2
BirtMath.round( 1545.50, -1 ) // 将返回 1550

以下示例将对 PriceEstimate 字段中的值进行舍入,以返回一个整数:

BirtMath.round( row["PriceEstimate"], 0 )

BirtMath.roundDown

此函数会将一个数向下舍入为指定的位数。

语法

Number BirtMath.roundDown( Number number, Number dec )

参数

number

Number 对象。要舍入的数。

dec

Number 对象。要将一个数向下舍入为的位数。如果省略此自变量,那么此函数将假定值为 0。

返回

Number 对象。向下舍入为指定位数的数。

示例

下列示例将显示此函数将为特定数返回的结果

BirtMath.roundDown( 1545.50 ) // 将返回 1545
BirtMath.roundDown( 1545.56, 1 ) // 将返回 1545.5
BirtMath.roundDown( 1545.23, 1 ) // 将返回 1545.2
BirtMath.roundDown( 1545.50, -1 ) // 将返回 1540

以下示例将对 PriceEstimate 字段中的值进行向下舍入,以返回一个整数:

BirtMath.roundDown( row["PriceEstimate"], 0 )

BirtMath.roundUp

此函数会将一个数向上舍入为指定的位数。

语法

Number BirtMath.roundUp( Number number, Number dec )

参数

number

Number 对象。要向上舍入的数。

dec

Number 对象。要将一个数向上舍入为的位数。如果省略此自变量,那么此函数将假定值为 0。

返回

Number 对象。向上舍入为指定位数的数。

示例

下列示例将显示此函数将为特定数返回的结果

BirtMath.roundUp( 1545.50 ) // 将返回 1546
BirtMath.roundUp( 1545.56, 1 ) // 将返回 1545.6
BirtMath.roundUp( 1545.23, 1 ) // 将返回 1545.3
BirtMath.roundUp( 1545.50, -1 ) // 将返回 1550

以下示例将对 PriceEstimate 字段中的值进行向上舍入,以返回一个整数:

BirtMath.roundUp( row["PriceEstimate"], 0 )

BirtMath.safeDivide

此函数将返回将一个数除以另一个数所得到的结果,并防止除数为 0 的情况。

语法

Number BirtMath.safeDivide( Number dividend, Number divisor, object ifZero )

参数

dividend

Number 对象。被除数。

divisor

Number 对象。除数。

ifZero

对象。除数为零时返回的值。

返回

Number 对象。Number 对象。将被除数除以除数所得到的数。

示例

以下示例将显示此函数将为特定数返回的结果:

BirtMath.safeDivide( 10, 2, 0 ) // 将返回 5
BirtMath.safeDivide( 10, 0, 0 ) // 将返回 0

以下示例将返回用 Revenue 字段中的值除以 Volume 字段中的值所得到的结果。如果 Volume 值为 0,那么此函数将返回 0。

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

BirtMath.subtract

此函数将返回用一个数减去另一个数所得到的结果。

语法

Number BirtMath.subtract( Number n1, Number n2 )

参数

n1

Number 对象。被减数。

n2

Number 对象。要减去 n1 的数。

返回

Number 对象。从 n1 中检索 n2 所得到的数。

示例

以下示例将显示减去特定数所得到的结果:

BirtMath.subtract( 12, 4 ) // 将返回 8
BirtMath.subtract( 4, 12) // 将返回 -8

以下示例将返回从 Total 字段中的值减去 Discount 字段中的值所得到的结果:

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

© Copyright Actuate Corporation 2013