Blitz3D+ Command Reference

Int value

Parameters

value - a number, or a string holding a number

Description

Converts a value to an integer.

Int is a prefix operator, so Int value and Int( value ) are the same. It does exactly what Blitz3D+ does automatically when you assign a float or a string to an integer variable, so n = Int( x# ) and n = x# behave identically.

Do not assume Int chops the fraction off - it rounds to the nearest whole number. Int( 2.4 ) is 2 but Int( 2.6 ) is 3, and Int( -2.6 ) is -3. Exact halves round to the nearest even number, so Int( 2.5 ) is 2 while Int( 3.5 ) is 4. That is the biggest single trap in Blitz maths, and it is not what Int means in most other BASICs. When you really want the fraction removed, use Floor for downwards or Ceil for upwards.

Applied to a string, Int reads as much of a valid whole number as it can and stops at the first character that cannot be part of one. Int( "10" ) is 10, Int( "3.7" ) is 3 because it stops at the dot, and Int( "junk3" ) is 0. That makes it a serviceable way to parse numbers out of a config line or a save file.

See also: Float, Str, Floor, Ceil, Sgn.

Example

; Int Example
; -----------

; Int converts a value to the NEAREST integer - the same conversion
; Blitz applies when you assign a float to an integer variable.
; Compare Ceil (always rounds up) and Floor (always rounds down).

Print "     value      Ceil     Floor   Int"
Print "------------------------------------"

; Each Read pulls the next sample value from the Data line below
For i=1 To 6
    Read v#
    Print RSet(v,10)+RSet(Ceil(v),10)+RSet(Floor(v),10)+RSet(Int(v),6)
Next

Data 2.3,2.7,2.5,-2.3,-2.7,-2.5

Print ""

; Values exactly halfway round to the nearest EVEN integer
Print "Int(2.5) = "+Int(2.5)+"   Int(3.5) = "+Int(3.5)+"  (halves go to the even neighbour)"

Print ""

; Int also converts strings, reading as many digits as it can
Print "Int of the string 10    = "+Int("10")
Print "Int of the string 3.7   = "+Int("3.7")+"  (stops at the decimal point)"
Print "Int of the string junk3 = "+Int("junk3")

Print ""
Print "Press any key to close the example"
WaitKey

End

Index