Format Specifiers in Julia
Last Updated :
15 Jul, 2025
Format Specifiers are used to format the Input and Output of a Programming language. It tell the compiler about the data type of the Input that is being passed to it. Julia contains a package Printf and using its macro @printf takes input string value to be formatted and accepts parameters as variables to apply to the string.
@printf is a macro in Julia, which has the following syntax:
@printf([io::IOstream], "%Fmt", args...)
Space format specifier
It is used to print a space before the variable value over which it is specified. The variable value can belong to any data type, numeric, or strings.
Julia
# declaring a variable string
val = "Space"
# importing module
using Printf
# adding space identifier after a string “Added”
@printf("Added, % s \n", val)
Output:
Added, Space
+ sign format specifier
On specifying a numeric value as argument with the %+d format, '+' sign is added before it. On specifying floating-point number as argument only the part before decimal is returned as output with +ve sign.
Julia
# declaring numeric value
num1 = 111
# importing required module
using Printf
# adding positive sign before numerical value
@printf("Adding %+d", num1)
# declaring floating point number
num2 = 10.1
# adding positive sign before floating number
@printf("Adding positive sign %+d", num2)
Output:
+111
+11
The floating numbers are converted to integers with a positive sign.
Left Justification (-) specifier
The output returned can be explicitly left-justified by applying a minus sign after the symbol %. The following code, for example, % 16.4f justifies the passed parameter in a field of 16 characters with two decimal places.
Julia
# importing module
using Printf
# declaring a floating point number
num1 = 10.1234
# left justifying the number
@printf("|%-16.4f|", num1)
Output:
|10.1234 |
%% format specifier
This format specifier can be used to insert a % sign. There are no parameters specified explicitly. It can be used for both strings or numbers.
Julia
# importing module
using Printf
# embedded in numerical values
@printf("2 %% 8 = 0")
# embedding in strings
@printf("hello %% gfg ")
Output:
2 % 8 = 0
hello % gfg
%x %X format specifier
This representation is used to donate a hexadecimal number of the specified numeric value in either lower case or upper case characters %x in lower case and %X in upper case. Only numerical values can be specified as parameters in this argument list.
Julia
# importing module
using Printf
# converting 100 to lower case hexadecimal
@printf("% x", 100)
# converting 13 to lower case hexadecimal
@printf("% x", 13)
# converting 234 to upper case hexadecimal
@printf("% X", 234)
Output:
64
d
EA
%o format specifier
This representation is used to denote the number specified as the argument in the octal form. The variable to be converted cannot be a floating-point number.
Julia
# importing module
using Printf
# converting 372 from decimal
# to octal representation
@printf("% o", 372)
# converting 9 to octal
@printf("% o", 9)
Output:
564
11
%e %E format specifier
This format specifier is used to express the value in a scientific way with %E expressing the output in upper case characters and %e in lower case characters.
Julia
using Printf
# converting numerical and floating numbers
# scientific notation in lower case
@printf("% e", 100)
@printf("% e", 12.56)
# converting numerical and floating numbers
# scientific notation in upper case
@printf("% E", 100)
@printf("% E", 99.78)
Output:
1.000000e+02
1.256000e+01
1.000000E+02
9.978000E+01
%f format specifier
Used to convert a number into floating-point integer values. Works both the numerical and decimal point numbers. It appends exactly 6 digits after the decimal, by default. We can specify the number of digits in %.xf parameter, where x indicates the number of digits.
Julia
using Printf
# converting numerical to decimal point
@printf("% f", 100)
# converting floating to decimal point
@printf("% f", 12.56)
# printing decimal number upto 4 places only
@printf("%.4f", 12.56)
Output:
100.000000
12.560000
12.5600
Similar Reads
Formatting of Strings in Julia Julia is a high-level language developed by people at Massachusetts Institute of Technology. It is an open-source, high-performance language. It has a simple syntax like Python but has speed and performance of C. String formatting is the process of evaluating a string literal using string interpolat
6 min read
Array Literals in Julia Julia is a high performance, dynamic programming language that is easy to use as it has a high-level syntax. It is free for everyone to use. Julia does not give a lot of attention to implementing arrays, so there is a lesser load on the compiler. An array in Julia is represented in a multi-dimension
4 min read
For loop in Julia For loops are used to iterate over a set of values and perform a set of operations that are given in the body of the loop. For loops are used for sequential traversal. In Julia, there is no C style for loop, i.e., for (i = 0; i Syntax: for iterator in range statements(s) end Here, 'for' is the keywo
2 min read
Vectors in Julia Vectors in Julia are a collection of elements just like other collections like Array, Sets, Dictionaries, etc. Vector are different from Sets because vectors are ordered collections of elements, and can hold duplicate values, unlike sets which require all the elements to be unique. Vectors are one-d
5 min read
Sets in Julia Set in Julia is a collection of elements just like other collections like arrays, dictionaries, etc. Sets are different from arrays because sets are unordered collections of unique elements whereas the order of elements is strictly remembered in Arrays. Also, Arrays and dictionaries may contain dupl
8 min read
Sets in Julia Set in Julia is a collection of elements just like other collections like arrays, dictionaries, etc. Sets are different from arrays because sets are unordered collections of unique elements whereas the order of elements is strictly remembered in Arrays. Also, Arrays and dictionaries may contain dupl
8 min read