round()

系统函数 MathLib.round 将数字或表达式四舍五入到最接近的值(例如,四舍五入到最接近的千位)并返回结果。

  MathLib.round(
    numericField mathLibNumber in
    [, powerOf10 mathLibInteger in
    ])
  returns (result mathLibTypeDependentResult)
  MathLib.round(numericExpression anyNumericExpression in
  returns (result mathLibTypeDependentResult)
result
任何数字或 HEX 项,如数学(系统字)中所述。四舍五入运算生成的值被转换为具有 result 的格式,并在 result 中返回。
由于四舍五入是按以下方式进行的,所以在这种情况下支持的最大长度是 31 而不是 32
  • 在比结果位的精度高 1 的精度位置,对 result 中的位加 5
  • 将结果截断

如果在计算中使用了超过 31 位,并且如果 EGL 在开发时无法确定违例,则在运行时会发生数字溢出。

numericField
任何数字或 HEX 项,如数学(系统字)中所述。
numericExpression
数字表达式,而不是简单的数字项。如果指定了运算符,则不能对 integer 指定值。

不能将 MathLib.round 与余数运算符(%)配合使用。

powerOf10
一个整数,它确定将数字四舍五入到的值:
  • 如果 integer 是正数,则将数字四舍五入到最接近于 10 的 powerOf10 次幂的值。例如,如果 integer 是 3,则将数字四舍五入到最接近的千位。
  • 如果 integer 是零或负数,情况亦如此;在那种情况下,将把数字四舍五入到指定的小数位数。

如果未指定 powerOf10,则 MathLib.round 将四舍五入到 result 中的小数位数。

integer 被定义为具有 INT 类型或者具有以下等效类型:BIN 类型,长度为 9 并且不带小数位。

示例

在下面的示例中,项 balance 被四舍五入为最接近的千:

balance = 12345.6789;
rounder = 3;
balance = MathLib.round(balance, rounder);
// The value of balance is now 12000.0000

在下面的示例中,使用 rounder 值 -2 来将 balance 四舍五入为带有两个小数位:

balance = 12345.6789;
rounder = -2;
balance = mathLib.round(balance, rounder);
// The value of balance is now 12345.6800

相关参考
EGL 库 MathLib

使用条款 | 反馈
(C) Copyright IBM Corporation 2000, 2005. All Rights Reserved.