print and println@printftry-catch for Exception HandlingfinallyOften, the data you encounter or generate isn't in the exact form you need for a particular task. For instance, user input from a console typically arrives as text (a string), but you might need to perform mathematical calculations with it, requiring it to be a number. Julia, like many programming languages, provides ways to convert values from one data type to another. This process is fundamental to writing flexible and correct programs.
Explicit type conversion is when you, the programmer, directly instruct Julia to change a value's type. This gives you precise control over how data is handled. The general syntax for this is straightforward: you use the name of the target data type as if it were a function, passing the value you want to convert as an argument.
Let's look at some common scenarios:
Converting Between Numeric Types
You'll frequently need to convert between different kinds of numbers, like integers and floating-point numbers.
Integer to Float: To convert an integer to a floating-point number, you can use Float64() (or other float types like Float32 if specific precision is needed).
julia> int_value = 10
10
julia> float_value = Float64(int_value)
10.0
julia> typeof(float_value)
Float64
Converting an integer to a float is generally safe as no information is lost.
Float to Integer: Converting a floating-point number to an integer uses Int(). When you do this, Julia truncates the decimal part, meaning it simply cuts off anything after the decimal point. It does not round to the nearest integer.
julia> pi_approx = 3.14159
3.14159
julia> integer_part = Int(pi_approx)
3
julia> another_float = 3.99
3.99
julia> Int(another_float) # Still truncates
3
Be mindful that converting from a float to an integer can result in a loss of information (the fractional part).
Converting Strings to Numbers
Data read from files or user input is often in string format. To use these as numbers, you need to "parse" them. Julia provides the parse() function for this.
julia> numeric_string = "123"
"123"
julia> my_integer = parse(Int, numeric_string)
123
julia> typeof(my_integer)
Int64
julia> float_string = "2.718"
"2.718"
julia> my_float = parse(Float64, float_string)
2.718
julia> typeof(my_float)
Float64
If you try to parse a string that doesn't represent a valid number of the target type, Julia will raise an error. For example, parse(Int, "hello") would fail. Handling such potential errors is a topic we'll cover in a later chapter.
Converting Numbers to Strings
The reverse, converting a number into its string representation, is also common, perhaps for display purposes or when writing to a file. The string() function handles this:
julia> lucky_number = 7
7
julia> string_representation = string(lucky_number)
"7"
julia> typeof(string_representation)
String
julia> eulers_number = 2.71828
2.71828
julia> string(eulers_number)
"2.71828"
Converting Strings to Booleans
For boolean values, you can parse strings like "true" or "false":
julia> true_string = "true"
"true"
julia> bool_val_true = parse(Bool, true_string)
true
julia> typeof(bool_val_true)
Bool
julia> parse(Bool, "false")
false
Parsing "1" or "0" for booleans is also supported by parse(Bool, ...).
Sometimes, Julia can automatically convert types for you, particularly in arithmetic operations involving different numeric types. This is called type promotion. The goal of promotion is usually to preserve precision by converting to a "wider" or more general type that can accommodate all values involved without loss of information.
Consider adding an integer and a floating-point number:
julia> result = 5 + 2.5
7.5
julia> typeof(result)
Float64
Here, the integer 5 was automatically promoted to the floating-point number 5.0 before the addition occurred, so the result 7.5 is also a Float64. This is sensible because if Julia had converted 2.5 to an integer (e.g., 2), the result would have been 7, losing the fractional part. Julia's promotion rules help prevent such inadvertent loss of precision in common operations.
An integer and a float combine in an operation, with the integer being promoted to a float, resulting in a float.
Type promotion makes writing mixed-type arithmetic code cleaner and less error-prone, as you often don't have to litter your code with explicit conversions for basic math.
Understanding type conversion is essential because:
readline() is always a string. If you expect a number, you must parse it.Float64 to Int, can lead to loss of data (the fractional part is truncated).parse(Int, "not a number")) will result in an error. Your program might stop if this isn't handled. We'll explore error handling later.Mastering data type conversion allows you to fluidly work with data from various sources and prepare it for any operation, forming a critical skill in your Julia programming toolkit. As you progress, you'll find these conversion techniques invaluable for building effective applications.
Was this section helpful?
© 2026 ApX Machine LearningEngineered with