Julia Language Convert numeric types to strings


Example

There are numerous ways to convert numeric types to strings in Julia:

julia> a = 123
123

julia> string(a)
"123"

julia> println(a)
123

The string() function can also take more arguments:

julia> string(a, "b")
"123b"

You can also insert (aka interpolate) integers (and certain other types) into strings using $:

julia> MyString = "my integer is $a"
"my integer is 123"

Performance Tip: The above methods can be quite convenient at times. But, if you will be performing many, many such operations and you are concerned about execution speed of your code, the Julia performance guide recommends against this, and instead in favor of the below methods:

You can supply multiple arguments to print() and println() which will operate on them exactly as string() operates on multiple arguments:

julia> println(a, "b")
123b

Or, when writing to file, you can similarly use, e.g.

open("/path/to/MyFile.txt", "w") do file
    println(file, a, "b", 13)
end

or

file = open("/path/to/MyFile.txt", "a")
println(file, a, "b", 13)
close(file)

These are faster because they avoid needing to first form a string from given pieces and then output it (either to the console display or a file) and instead just sequentially output the various pieces.

Credits: Answer based on SO Question What's the best way to convert an Int to a String in Julia? with Answer by Michael Ohlrogge and Input from Fengyang Wang