ReadyBASIC
BASIC Language Reference Manual > Command and Function Reference >

TRUNC Function

Summary

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

Syntax

TRUNC(floating-point numeric expression)

Remarks and Examples

The TRUNC function allows you to convert a floating-point value (a number with decimal places) to an integer value (a "whole" number without any decimal places).

TRUNC simply "truncates" the given value by trimming off the decimal places. For example, TRUNC(25.789) will give the integer value 25. The effect is the same for negative numbers: TRUNC(-9.9) returns the value -9.

The INT function also uses truncation to convert floating-point values to integer values. The INT TRUNC functions therefore have an identical effect. The functions are synonyms.

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.