Julia Type Conversion Tutorial
last modified November 18, 2026
Julia is dynamically typed, yet every value has a concrete type. Often a value
must be turned into another type: text read from a file becomes a number, an
integer is used where a float is expected, or a value must satisfy a declared
type. This tutorial covers the tools Julia provides for that — type assertions,
convert, constructors, parsing, and promotion.
We also look at the difference between conversion, which changes one value into another, and promotion, which brings several values to a common type. The basic type hierarchy is introduced in Julia data types; here we go deeper into the conversion mechanisms. All examples were verified with Julia 1.13.4.
Type Assertions with ::
A type assertion tells Julia to check that an expression has a given type. The
:: operator can be attached to a value or to a variable declaration;
if the check fails, an error is raised.
x = 10::Int println(x, " ", typeof(x)) y::Float64 = 3 println(y, " ", typeof(y))
The first line asserts that 10 is an Int, which it is.
The second line declares y with the type Float64 and
assigns the integer 3 to it; Julia converts the value so that the
assertion holds. Writing y::String = 3 would instead fail, because
no conversion from an integer to a string exists.
$ julia main.jl 10 Int64 3.0 Float64
isa as a Runtime Check
The isa operator asks whether a value belongs to a type at run time.
It returns a boolean rather than converting anything, which makes it useful for
branching on a value's kind.
println(10 isa Int)
println(3.5 isa Integer)
println(3.5 isa Real)
println("abc" isa AbstractString)
println(10 isa Number)
A float is not an Integer, but it is a Real and a
Number; a string is an AbstractString. The operator can
also be written in function form as isa(10, Int). The type hierarchy
behind these results is explained in Julia data
types and Julia abstract types.
$ julia main.jl true false true true true
The convert Function
The convert function produces a value of a requested type from a
value of another type. It is the explicit, general-purpose way to ask for a
conversion and works whenever Julia has a conversion rule for the pair of types.
println(convert(Int, 3.0), " ", typeof(convert(Int, 3.0))) println(convert(Float64, 3), " ", typeof(convert(Float64, 3))) println(convert(String, "abc")) println(convert(Int, true))
Converting 3.0 to Int succeeds because the value has no
fractional part. Converting 3 to Float64 gives
3.0. Converting a string to String is a harmless
no-op, and a boolean becomes 1 or 0 because
Bool is a subtype of Integer.
$ julia main.jl 3 Int64 3.0 Float64 abc 1
Constructor Conversion
A type is also a function: calling it with a value of another type converts that value. This is the most common conversion syntax in everyday Julia code.
println(Int(3.0), " ", typeof(Int(3.0))) println(Float64(3), " ", typeof(Float64(3))) println(Bool(1), " ", Bool(0)) println(String(['h', 'i']))
For numeric types, T(x) is equivalent to convert(T, x).
Some constructors do more than convert: Bool(1) maps nonzero
integers to true, and String accepts an array of
characters and joins it into "hi". The result's type is shown beside
each value.
$ julia main.jl 3 Int64 3.0 Float64 true false hi
Conversion and Promotion
Promotion is a related but distinct idea: it converts several values to one
common type so that an operation can combine them. The promote
function returns a tuple of converted values, while promote_type
reports the common type without converting anything.
println(promote(1, 2.0)) println(typeof(promote(1, 2.0)[1])) println(promote(Int8(1), Int16(2))) println(promote_type(Int, Float64)) println(1 + 2.0, " ", typeof(1 + 2.0))
An integer and a float become two Float64 values. Two small
integers, Int8 and Int16, meet at the wider
Int16 type. The final line shows why this matters: adding an integer
and a float produces a Float64 result. Promotion is discussed in
more detail in Julia data types.
$ julia main.jl (1.0, 2.0) Float64 (1, 2) Float64 3.0 Float64
Parsing Strings with parse and tryparse
Conversion turns a value of one type into another; parsing builds a value from
its textual representation. The parse function raises an error on
bad input, while tryparse returns nothing instead.
println(parse(Int, "42")) println(parse(Float64, "3.14")) println(parse(Bool, "true")) println(tryparse(Int, "42")) println(tryparse(Int, "x"))
The first three lines parse an integer, a float, and a boolean from text. The
last two lines contrast the safe variant: with valid input it returns the parsed
value, and with invalid input it returns nothing — the special value
that means "no value". Use tryparse for input that might be malformed,
such as data read from a user or a file.
$ julia main.jl 42 3.14 true 42 nothing
Building Strings with string and repr
The reverse of parsing is turning a value into text. The string
function produces a human-readable string, while repr produces a
form that resembles Julia code.
println(string(42), " ", typeof(string(42))) println(string(3.14)) println(repr(3.14)) println(string(true))
Every value can be passed to string, and the result is always a
String. For numbers the two forms coincide here; for strings and
other types repr adds the quotation marks that would be needed to
write the value as a literal. String handling is covered further in
Julia strings.
$ julia main.jl 42 String 3.14 3.14 true
Inexact Conversions and Rounding
A float with a fractional part cannot be converted directly to an integer, because
that would lose information. Julia provides floor, round,
ceil, and trunc with an explicit target type to state
how the fraction should be handled.
println(floor(Int, 3.7)) println(round(Int, 3.7)) println(ceil(Int, 3.7)) println(trunc(Int, -3.7))
These functions round down, to the nearest integer, up, and toward zero
respectively. For 3.7 the results are 3, 4, and 4;
trunc(Int, -3.7) gives -3 because truncation discards
the fractional part without moving toward negative infinity. Supplying the type
as the first argument makes the result an Int.
$ julia main.jl 3 4 4 -3
Convenience Conversion Helpers
Julia also offers small helpers for common conversions: float
promotes a number to a floating-point type, big selects an
arbitrary-precision type, and oftype converts a value to the type of
another value.
println(float(3), " ", typeof(float(3))) println(Int32(3.0), " ", typeof(Int32(3.0))) println(big(3), " ", typeof(big(3))) println(oftype(1, 2.0), " ", typeof(oftype(1, 2.0)))
Here float(3) produces 3.0, Int32(3.0)
requests a 32-bit integer explicitly, and big(3) creates a
BigInt. The call oftype(1, 2.0) converts 2.0
to the type of 1, yielding the integer 2. These helpers
are useful when a function should preserve or widen the caller's numeric type.
$ julia main.jl 3.0 Float64 3 Int32 3 BigInt 2 Int64
When Conversion Fails
Not every conversion is possible. Julia raises an exception when a requested
conversion cannot be performed, and try/catch lets a
program react to that instead of stopping.
println(Int8(100))
println(UInt8(200))
println(Int(3.0) === convert(Int, 3.0))
try
println(convert(Int, 3.7))
catch e
println(typeof(e))
end
The first two lines fit inside the ranges of Int8 and
UInt8. The third line confirms that constructor conversion and
convert give the same result. Converting 3.7 to an
Int throws an InexactError, which the
catch block reports; use a rounding function from the previous
section in that situation. Error handling is covered in
Julia exceptions.
$ julia main.jl 100 200 true InexactError
Best Practices for Type Conversion
A few simple habits keep conversions predictable and easy to read.
- Prefer
T(x)for numeric conversions: it is the idiomatic form and is equivalent toconvert(T, x). - Use
tryparsefor untrusted text: it returnsnothingon bad input instead of throwing. - Be explicit about rounding: choose
floor,round,ceil, ortruncwhen a fractional part must be dropped. - Use
isafor checks, assertions for guarantees:isabranches, while::enforces a type.
Source
Julia Conversion and Promotion documentation
In this article we have worked with type conversion in Julia: assertions with
::, runtime checks with isa, convert,
constructors, parsing with parse and tryparse, promotion,
and rounding helpers. The underlying type system is covered in
Julia data types and
Julia abstract types; errors are described in
Julia exceptions.
Author
List all Julia tutorials.