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.
# 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.
# 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.
# 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.
# 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.
# 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.
# 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.
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.
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