Format Specifiers in Julia
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
val = "Space"
using Printf
@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
num1 = 111
using Printf
@printf ( "Adding %+d" , num1)
num2 = 10.1
@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
using Printf
num1 = 10.1234
@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
using Printf
@printf ( "2 %% 8 = 0" )
@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
using Printf
@printf ( "% x" , 100 )
@printf ( "% x" , 13 )
@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
using Printf
@printf ( "% o" , 372 )
@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
@printf ( "% e" , 100 )
@printf ( "% e" , 12.56 )
@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
@printf ( "% f" , 100 )
@printf ( "% f" , 12.56 )
@printf ( "%.4f" , 12.56 )
|
Output:
100.000000
12.560000
12.5600
Last Updated :
12 Oct, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...