Casting a Variable

Sometimes, you can use a variable in multiple ways:

  • Float as an integer

  • Integer as a float

  • "" can be false or 0

    • This is called an empty string

Other times, the variable has to be cast into another type using an extra step.

This varies widely among languages, so it's best to look it up.

Examples:

Processing example:

float amount = 1.0;
int amountAsInt = int(amount);

// now amountAsInt can be used where an integer is needed

C# example:

int count = 3;
string countAsString = count.ToString();

// now countAsString can be used where a string is needed

Python example:

count = 3
countAsFloat = float(count)

# now countAsFloat can be used where a float is needed

Last updated