ReadyBASIC
BASIC Language Reference Manual > Command and Function Reference >

ROUND Function

Summary

Rounds a floating-point numeric value to an integer number value.

Syntax

ROUND(floating-point numeric expression)

Remarks and Examples

The ROUND function rounds a floating-point value (a number with decimal places) to an integer value (a "whole" number without any decimal places).

If the fractional part (the part after the decimal point) is equal to or greater than 0.5, the next higher integer will be return (rounding up). If the fractional part is below 0.5, the next lower integer will be used (rounding down). For example, ROUND(11.5) gives 12, and ROUND(11.49) gives 11.

For negative numbers, the logic is exactly the same, although it may not appear so at first glance. The rule remains: if the fractional part is equal to or greater than 0.5, then the next higher integer will be used. So ROUND(-11.5) gives -11 and not -12, because -11 is higher than -12 on the number line. ROUND(-11.1) also gives -11, but ROUND(-11.9) gives -12.

The following sample program compares the effects of the INT, TRUNC, CEIL, FLOOR, and ROUND functions, all of which can be used to convert a floating-point value to an integer.

10 OPTION TABWIDTH 10
100 DIM VALUES(6)
110 VALUES(1) = 3.1
120 VALUES(2) = 3.5
130 VALUES(3) = 3.9
140 VALUES(4) = -7.1
150 VALUES(5) = -7.5
160 VALUES(6) = -7.9
200 PRINT "Function",
210 FOR X = 1 TO 6
220 PRINT VALUES(X),
230 NEXT X
240 PRINT
300 PRINT "INT",
310 FOR X = 1 TO 6
320 PRINT INT(VALUES(X)),
330 NEXT X
340 PRINT
400 PRINT "TRUNC",
410 FOR X = 1 TO 6
420 PRINT TRUNC(VALUES(X)),
430 NEXT X
440 PRINT
500 PRINT "CEIL",
510 FOR X = 1 TO 6
520 PRINT CEIL(VALUES(X)),
530 NEXT X
540 PRINT
600 PRINT "FLOOR",
610 FOR X = 1 TO 6
620 PRINT FLOOR(VALUES(X)),
630 NEXT X
640 PRINT
700 PRINT "ROUND",
710 FOR X = 1 TO 6
720 PRINT ROUND(VALUES(X)),
730 NEXT X
740 PRINT
RUN
Function 3.1       3.5       3.9       -7.1      -7.5      -7.9
INT      3         3         3         -7        -7        -7
TRUNC    3         3         3         -7        -7        -7
CEIL     4         4         4         -7        -7        -7
FLOOR    3         3         3         -8        -8        -8
ROUND    3         4         4         -7        -7        -8



Copyright 2006-2008, Kevin Matz, All Rights Reserved.