| A math function that converts a number to an integer by rounding the fractional part of the expression.
Syntax: ROUND(numeric-expression) A% = ROUND(3.49) '-- Returns 3 A% = ROUND(3.50) '-- Returns 4 Details:
If the argument is negative, then Round always rounds
toward zero. I.E. Round(-3.99) returns -3 instead of -4. Also IEEE choses round-to-even like this
-2.5 rounds to -2
-1.5 rounds to -2
-0.5 rounds to 0
0.5 rounds to 0
1.5 rounds to 2
2.5 rounds to 2
If you need different precision then try one of these routines.
FUNCTION RoundOff(z AS DOUBLE , accuracy AS INTEGER) AS DOUBLE
DEFDBL scaleracc = 10^accuracy
IF z < 0 THEN
RESULT = CEIL(z * scaleracc - 0.500000#) / scaleracc
ELSE
RESULT = FLOOR(z * scaleracc + 0.500000#) / scaleracc
END IF
END FUNCTION
RoundOff(x, 0) for x:
-2.5 rounds to -3
-1.5 rounds to -2
-0.5 rounds to -1
0.5 rounds to 1
1.5 rounds to 2
2.5 rounds to 3
'This version is IEEE binary round-to-even compatible:
FUNCTION rqROUND_TC(Value#) AS LONG
DEFDBL ABSVal# = ABS(Value#)
DEFINT SGNVal& = SGN(Value#)
DEFINT FIXVal& = FIX(ABSVal#)
DEFDBL FRACVal# = FRAC(ABSVal#)
DEFINT AddVal& = (((FRACVal# = .5) AND FIXVal&) OR (FRACVal# > .5)) AND 1
FIXVal& += AddVal&: IF SGNVal& = -1 THEN FIXVal& = -FIXVal&
rqROUND = FIXVal&
END FUNCTION
' this is faster but still rounds to even
FUNCTION rqROUND2(Value AS DOUBLE) AS INTEGER
IF Value < 0 THEN
RESULT = -1 * ROUND(ABS(Value))
ELSE
RESULT = ROUND(Value)
END IF
END FUNCTION
| |