Round to Nearest Even

In Full BASIC, the supplied function ROUND(x,n) is defined by INT(x*10^n+0.5)/10^n.
If you want to use Banker's rounding method, you are recommended to define an external function as follows.
The meanings of two parameters are the same as those of the supplied function ROUND.
If you use the external function ROUND10, insert

DECLARE EXTERNAL FUNCTION ROUND10

at the first of the program and contain the following below the END line.

EXTERNAL FUNCTION ROUND10(x,n)
REM Round to Nearest Even
LET x=x*10^n
IF MOD(x,1)<0.5 THEN
   LET x=INT(x)
ELSEIF MOD(x,1)>0.5 THEN
   LET x=CEIL(x)
ELSEIF MOD(INT(x),2)=0 THEN
   LET x=INT(x)
ELSE 
   LET x=CEIL(x)
END IF
LET ROUND10=x/10^n
END FUNCTION

If you use this in some external procedures, each external procedure must contain the DECLARE EXTERNAL FUNCTION statement.

Note.
The name of the function can be ROUND. In such case, the function name written in the LET statement just before END FUNCTION line must be modified, too. But the supplied function ROUND cannot be used in the program unit that have DECLARE EXTERNAL FUNCTION ROUND.

Supplement
On the binary mode, ROUND(x) with one parameter rounds to the nearest integer. (This type of ROUND is not contained in the Full BASIC standard.)
Using this, we can define a function that rounds x into n bits below the floating point as follows.

DEF ROUND2(x,n)=ROUND(x*2^n)/2^n

Back