Scripting > UNICOM Intelligence Function Library > Mathematical functions > Round
 
Round
Returns a number rounded to a specified number of decimal places or significant digits.
Syntax
Round(Val [, Digits [, Policy]])
Parameters
Val
Type: Double
The value to be rounded.
Digits
Type: Long
Optional. The number of decimal places or significant digits in the result. Treated as 0 if omitted or is < 0.
Policy
Type: Long
Optional. The type of rounding to use: see below. Treated as 0 if omitted or is < 0.
(return)
Type: Double
Val rounded as specified.
Remarks
The return value is the number nearest to Val with either the required number of digits after the decimal point or the required number of significant digits, depending on Policy. If Policy is 0, Val is rounded to the number of decimal places specified by Digit. If Policy is 0 and Val is exactly halfway between two possible rounded values (a “borderline” case) then this function performs round to even. This means that of the two possible rounded values, the one that has an even number as the last significant digit is returned. For example, 15.25 is rounded to 15.2 rather than 15.3.
If Val is 0.0, the return value is 0.0. If the current data value is NULL, Val is 0.0 and the return value is 0.0.
If Digits is 0 and Policy is 0 or an even number, Val is rounded to an integer. If Digits is 0 and Policy is an odd number, Val is rounded to one significant digit.
Possible values for Policy
To combine values, add them together:
1
Round Val to a number of significant digits rather than to a number of decimal places.
2
If Val is exactly halfway between the two nearest values of the required precision, round to the higher value rather than rounding to even. However, if value 4 below is also set, round to the lower value of the two nearest values.
4
If Val is exactly halfway between the two nearest values of the required precision, round to odd rather than rounding to even. However, if value 2 above is also set, round to the lower of the two nearest values.
8
If Val is negative and exactly halfway between the two nearest values of the required precision, round the absolute value and make its sign negative. If this value is set, “higher” and “lower” in values 2 and 4 above means “having a larger absolute value” and “having a smaller absolute value” respectively. This value has no effect if value 2 is not set.
Examples
Function call
Val
Digits
Result
Notes
Round(Val, Digits)
1234.5678
2
1234.57
Rounded to 2 decimal places
Round(Val, Digits)
28.613
1
28.6
Rounded to 1 decimal place
Round(Val, Digits)
62.75
1
62.8
Borderline case, rounded to even
Round(Val, Digits)
62.85
1
62.8
Borderline case, rounded to even
Round(Val, Digits)
1234.5678
NULL
1235
Rounded to an integer
Function call
Val
Digits
Policy
Result
Notes
Round(Val, Digits, Policy)
1234.5678
3
0
1234.568
Rounded to 3 decimal places
Round(Val, Digits, Policy)
1234.5678
3
1
1230
Rounded to 3 significant digits
Round(Val, Digits, Policy)
1234.5678
6
1
1234.57
Rounded to 6 significant digits
Round(Val, Digits, Policy)
1234.5678
0
1
1000
Rounded to 1 significant digit
Round(Val, Digits, Policy)
62.85
1
0
62.8
Borderline case, rounded to even
Round(Val, Digits, Policy)
62.85
1
2
62.9
Borderline case, rounded to higher value
Round(Val, Digits, Policy)
-62.85
1
2
-62.8
Borderline case, rounded to higher value
Round(Val, Digits, Policy)
-62.85
1
6
-62.9
Borderline case, rounded to lower value
Round(Val, Digits, Policy)
135
2
1
140
Borderline case, rounded to even
Round(Val, Digits, Policy)
135
2
5
130
Borderline case, rounded to odd
Round(Val, Digits, Policy)
-107.135
2
2
-107.13
Borderline case, rounded to higher value
Round(Val, Digits, Policy)
-107.135
2
10
-107.14
Borderline case, rounded to absolute higher value and made negative
The following mrScriptBasic example sets variable myResult to the value of myVariable rounded to two decimal places:
Dim myVariable, myResult
myVariable = 255.2914
myResult = myVariable.Round(2) ' Returns 255.29
Double value type comparisons
You may receive unexpected results when comparing double value types. In the following example two questions are defined and the answer to the second question is based on the answer to the first question:
Metadata(en-US, Question, Label)
QS1 "First Question"
style(
Width = "4em"
)
double [0.01 .. 99.99];
QS2 "Second Question > Q1"
style(
Width = "4em"
)
double [0.01 .. 99.99];
End Metadata
The routing for this example is:
Routing(Web)
QS1.Ask()
QS2.Validation.MinValue=(QS1.Response+0.01)

Qs2.Ask()
End Routing
Assuming 0.05 is entered as the answer to first question, and the minimum value for the second question is set to 0.06, when 0.06 is entered as the answer to second question, you may receive an error similar to: Answer '0.06' is not in range '0.06 - 99.99'. This is a result of double precision; two double values cannot be exactly compared.
You can utilize the Round function to round the double value. In the following example, the routing is modified as follows:
Routing(Web)
Const ROUND_UP = 10
QS1.Ask()
QS2.Validation.MinValue= Round(QS1.Response+0.01, 2, ROUND_UP)

Qs2.Ask()
End Routing
The Round function compares the two double values with specific precision.
See also
Mathematical functions