Type Conversion Functions

BambooBasic provides built-in functions to convert between different data types. These functions are essential for mixing different types in expressions and displaying values.



ToString() - Convert to String

Converts Int or Double values to String format for display or concatenation.

Syntax:

ToString:String(value:Int)
ToString:String(value:Double)

Examples:

Function Main()
    Local age:Int = 25
    Local pi:Double = 3.14159

    ; Convert Int to String
    Print "Age: " & ToString(age)

    ; Convert Double to String
    Print "Pi: " & ToString(pi)

    ; Use in concatenation
    Local message:String = "You are " & ToString(age) & " years old"
    Print message

    Return False
EndFunction

Output:

Age: 25
Pi: 3.14159
You are 25 years old


ToInt() - Convert to Integer

Converts String or Double values to Int format.

Syntax:

ToInt:Int(value:String)
ToInt:Int(value:Double)

Examples:

Function Main()
    ; Convert String to Int
    Local numberStr:String = "42"
    Local number:Int = ToInt(numberStr)
    Print "Number: " & ToString(number)

    ; Convert Double to Int (truncates)
    Local pi:Double = 3.14159
    Local rounded:Int = ToInt(pi)
    Print "Pi as Int: " & ToString(rounded)

    ; String with spaces
    Local ageStr:String = "  25  "
    Local age:Int = ToInt(ageStr)
    Print "Age: " & ToString(age)

    Return False
EndFunction

Output:

Number: 42
Pi as Int: 3
Age: 25

Notes:



ToDouble() - Convert to Double

Converts String or Int values to Double (floating-point) format.

Syntax:

ToDouble:Double(value:String)
ToDouble:Double(value:Int)

Examples:

Function Main()
    ; Convert String to Double
    Local piStr:String = "3.14159"
    Local pi:Double = ToDouble(piStr)
    Print "Pi: " & ToString(pi)

    ; Convert Int to Double
    Local whole:Int = 42
    Local decimal:Double = ToDouble(whole)
    Print "As Double: " & ToString(decimal)

    ; Fractional strings
    Local heightStr:String = "1.75"
    Local height:Double = ToDouble(heightStr)
    Print "Height: " & ToString(height) & " meters"

    Return False
EndFunction

Output:

Pi: 3.14159
As Double: 42
Height: 1.75 meters

Notes:



Chr() - Convert Code to Character

Converts an Int ASCII code to a single-character String.

Syntax:

Chr:String(code:Int)

Examples:

Function Main()
    ; Convert ASCII codes to characters
    Local letterA:String = Chr(65)
    Print "ASCII 65: " & letterA

    Local letterZ:String = Chr(90)
    Print "ASCII 90: " & letterZ

    ; Convert lowercase
    Local lettera:String = Chr(97)
    Print "ASCII 97: " & lettera

    ; Special characters
    Local newline:String = Chr(10)
    Local tab:String = Chr(9)
    Local space:String = Chr(32)

    ; Build a string from codes
    Local greeting:String = Chr(72) & Chr(101) & Chr(108) & Chr(108) & Chr(111)
    Print greeting  ; Prints "Hello"

    Return False
EndFunction

Output:

ASCII 65: A
ASCII 90: Z
ASCII 97: a
Hello

Common ASCII Codes:

Code Character Description
9 \t Tab
10 \n Newline
13 \r Carriage Return
32 (space) Space
48-57 0-9 Digits
65-90 A-Z Uppercase letters
97-122 a-z Lowercase letters


Asc() - Convert Character to Code

Converts the first character of a String to its ASCII code (Int).

Syntax:

Asc:Int(text:String)

Examples:

Function Main()
    ; Get ASCII code of characters
    Local codeA:Int = Asc("A")
    Print "Code for 'A': " & ToString(codeA)

    Local codea:Int = Asc("a")
    Print "Code for 'a': " & ToString(codea)

    ; Get code from first character
    Local word:String = "Hello"
    Local firstCode:Int = Asc(word)
    Print "First character of 'Hello': " & ToString(firstCode)

    ; Space character
    Local spaceCode:Int = Asc(" ")
    Print "Code for space: " & ToString(spaceCode)

    Return False
EndFunction

Output:

Code for 'A': 65
Code for 'a': 97
First character of 'Hello': 72
Code for space: 32

Notes:



Practical Example: Input Validation
Function Main()
    Print "=== Age Calculator ==="
    Print ""

    ; Get age as string
    Local ageStr:String
    Input "Enter your age: ", ageStr

    ; Convert to Int
    Local age:Int = ToInt(ageStr)

    ; Validate
    If age <= 0 Or age > 150 Then
        Print "Invalid age entered!"
    Else
        ; Calculate year born (approximately)
        Local currentYear:Int = 2026
        Local yearBorn:Int = currentYear - age

        Print "You are " & ToString(age) & " years old"
        Print "You were born around " & ToString(yearBorn)

        ; Convert to message
        Local message:String = "In 10 years, you'll be " & ToString(age + 10)
        Print message
    EndIf

    Return False
EndFunction


Practical Example: Character Processing
Function IsUppercase:Int(ch:String)
    Local code:Int = Asc(ch)
    If code >= 65 And code <= 90 Then
        Return True
    Else
        Return False
    EndIf
EndFunction

Function ToUppercase:String(ch:String)
    Local code:Int = Asc(ch)
    ; If lowercase (97-122), convert to uppercase
    If code >= 97 And code <= 122 Then
        Return Chr(code - 32)
    Else
        Return ch
    EndIf
EndFunction

Function Main()
    Print "Testing character functions:"
    Print ""

    ; Test IsUppercase
    Print "Is 'A' uppercase? " & ToString(IsUppercase("A"))
    Print "Is 'a' uppercase? " & ToString(IsUppercase("a"))

    ; Test ToUppercase
    Print "Uppercase 'a': " & ToUppercase("a")
    Print "Uppercase 'z': " & ToUppercase("z")
    Print "Uppercase 'A': " & ToUppercase("A")

    Return False
EndFunction

Output:

Testing character functions:

Is 'A' uppercase? 1
Is 'a' uppercase? 0
Uppercase 'a': A
Uppercase 'z': Z
Uppercase 'A': A


Type Conversion Summary
Function Input Type Output Type Purpose
ToString() Int, Double String Convert numbers to text
ToInt() String, Double Int Convert to integer
ToDouble() String, Int Double Convert to decimal
Chr() Int String ASCII code to character
Asc() String Int Character to ASCII code


Key Points

See Also

Examples

See the Language Examples for demonstrations using type conversion.


BambooBasic © 2026 Michael Denathorn