Ad space (banner)
๐ŸŸฃJulia Lessons
Lesson 31 / 68

The basics of type conversion (casting)

This lesson covers type conversion in Julia, with the aim of converting between strings and numbers.

Converting a string to a number, or a number to a string — type conversion (casting) — is a basic programming operation. You use parse(), string(), and friends.

The sample code converts the string "123" to an integer with parse(Int, "123") and uses it in a calculation, then converts the number 45 to a string with string(45) to join it to another string. Note that parse() takes the target type as its first argument.

A common early stumble is not allowing for a conversion that fails. Passing parse() a string that is not a number raises an exception, so tryparse() is the safer choice.

In professional work, type conversion is a daily occurrence — using input as a number, or turning a calculated result into text for display.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Julia directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)